I''ve got a list of items that I want to persist to the db if the user is logged in, but just keep in memory if he''s not logged in. I add items to the list with a simple "list << item", then noticed that if I save the list, all the objects get counted double, because now it''s in memory but also in the db. So I got the idea that when I save the list to the db, I''ll just reload it to clear out all the attributes. def after_save reload end Simple stuff. No syntax errors, and it gives me the exact behavior I''m looking for. I just want to know if that''s horrible practice, or there''s a better way to do it. Thanks for any response Pat
I''ve run into a problem, though I don''t think it''s at all because I''m calling reload. I''m playing around with the Depot app and want to persist the cart if the user''s logged in, but just keep it in memory when they''re not. If they add duplicate items, then it''ll update the quantity automatically, like in the book. def add_item(item, quantity = 1, options = nil) ## TODO ## Implement code to combine duplicate items if the options are the same dup_item = cart_items.detect {|i| i.item_id == item.id} if dup_item dup_item.quantity += quantity else cart_item = CartItem.new cart_item.quantity = quantity cart_item.cart = self cart_item.set_options options unless options.nil? || options.size == 0 cart_item.item = item cart_items << cart_item end end That works fine when I don''t save the cart, but if I do, the quantity is always 1. It doesn''t matter if I pass in a different value for quantity, or even specify a new default. The quantity is just always 1...and I don''t get it at all. Any ideas on what''s going on? On 7/9/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''ve got a list of items that I want to persist to the db if the user > is logged in, but just keep in memory if he''s not logged in. I add > items to the list with a simple "list << item", then noticed that if I > save the list, all the objects get counted double, because now it''s in > memory but also in the db. So I got the idea that when I save the > list to the db, I''ll just reload it to clear out all the attributes. > def after_save > reload > end > > Simple stuff. No syntax errors, and it gives me the exact behavior > I''m looking for. I just want to know if that''s horrible practice, or > there''s a better way to do it. Thanks for any response > > Pat >
Alright, so I figured out the problem. Turns out I had to remove the object from the array, modify it, then add it back on. So to my original question. Is it alright to call reload after I save the cart object? Gives me the behavior I want, just not sure if it''ll cause me headaches down the road. On 7/9/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''ve run into a problem, though I don''t think it''s at all because I''m > calling reload. > > I''m playing around with the Depot app and want to persist the cart if > the user''s logged in, but just keep it in memory when they''re not. If > they add duplicate items, then it''ll update the quantity > automatically, like in the book. > > def add_item(item, quantity = 1, options = nil) > ## TODO ## Implement code to combine duplicate items if the options > are the same > dup_item = cart_items.detect {|i| i.item_id == item.id} > if dup_item > dup_item.quantity += quantity > else > cart_item = CartItem.new > cart_item.quantity = quantity > cart_item.cart = self > cart_item.set_options options unless options.nil? || options.size == 0 > cart_item.item = item > cart_items << cart_item > end > end > > That works fine when I don''t save the cart, but if I do, the quantity > is always 1. It doesn''t matter if I pass in a different value for > quantity, or even specify a new default. The quantity is just always > 1...and I don''t get it at all. Any ideas on what''s going on? > > > > On 7/9/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I''ve got a list of items that I want to persist to the db if the user > > is logged in, but just keep in memory if he''s not logged in. I add > > items to the list with a simple "list << item", then noticed that if I > > save the list, all the objects get counted double, because now it''s in > > memory but also in the db. So I got the idea that when I save the > > list to the db, I''ll just reload it to clear out all the attributes. > > def after_save > > reload > > end > > > > Simple stuff. No syntax errors, and it gives me the exact behavior > > I''m looking for. I just want to know if that''s horrible practice, or > > there''s a better way to do it. Thanks for any response > > > > Pat > > >