I''m having a little trouble with this for some reason. Can someone tell me why I can do this... price = Price.find(5) price.destroy but not this... product.prices.each do |p| p.destroy end At the end of that latter piece of code, product.prices.size still equals 2. For the record, I do know about delete_all, there are some conditionals in my actual code that decide if an object gets deleted or not. I''m just trying to figure out why my objects aren''t being destroyed when inside a loop like that. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I think my problem is that the relationship is a many to many. What would be the best way to conditionally remove records this way? On Jul 12, 10:17 am, Josh <jjkie...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m having a little trouble with this for some reason. > > Can someone tell me why I can do this... > > price = Price.find(5) > price.destroy > > but not this... > > product.prices.each do |p| > p.destroy > end > > At the end of that latter piece of code, product.prices.size still > equals 2. > For the record, I do know about delete_all, there are some > conditionals in my actual code that decide if an object gets deleted > or not. I''m just trying to figure out why my objects aren''t being > destroyed when inside a loop like that.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Yeah, sorry about this one. my product object must have been cached inside of the console. It was deleting the objects from the database and when the console was reloaded, the values were correct. On Jul 12, 2007, at 12:05 PM, Philip Hallstrom wrote:> >> I''m having a little trouble with this for some reason. >> >> Can someone tell me why I can do this... >> >> price = Price.find(5) >> price.destroy >> >> but not this... >> >> product.prices.each do |p| >> p.destroy >> end >> >> At the end of that latter piece of code, product.prices.size still >> equals 2. >> For the record, I do know about delete_all, there are some >> conditionals in my actual code that decide if an object gets deleted >> or not. I''m just trying to figure out why my objects aren''t being >> destroyed when inside a loop like that. > > What''s the database say after you do that? And what happens if you > follow > that up with a: > > product.reload > > I''d be really surprised if the database wasn''t adjusted after you did > that, but can see that product wouldn''t know it''s been updated as > ''p'' is > local and not really tied to ''product''. > > -philip > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> I''m having a little trouble with this for some reason. > > Can someone tell me why I can do this... > > price = Price.find(5) > price.destroy > > but not this... > > product.prices.each do |p| > p.destroy > end > > At the end of that latter piece of code, product.prices.size still > equals 2. > For the record, I do know about delete_all, there are some > conditionals in my actual code that decide if an object gets deleted > or not. I''m just trying to figure out why my objects aren''t being > destroyed when inside a loop like that.What''s the database say after you do that? And what happens if you follow that up with a: product.reload I''d be really surprised if the database wasn''t adjusted after you did that, but can see that product wouldn''t know it''s been updated as ''p'' is local and not really tied to ''product''. -philip --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Yeah, sorry about this one. my product object must have been cached > inside of the console. > > It was deleting the objects from the database and when the console > was reloaded, the values were correct.I think the same will happen within Rails/web as well. Try the product.reload to see if that makes a difference.> > On Jul 12, 2007, at 12:05 PM, Philip Hallstrom wrote: > >> >>> I''m having a little trouble with this for some reason. >>> >>> Can someone tell me why I can do this... >>> >>> price = Price.find(5) >>> price.destroy >>> >>> but not this... >>> >>> product.prices.each do |p| >>> p.destroy >>> end >>> >>> At the end of that latter piece of code, product.prices.size still >>> equals 2. >>> For the record, I do know about delete_all, there are some >>> conditionals in my actual code that decide if an object gets deleted >>> or not. I''m just trying to figure out why my objects aren''t being >>> destroyed when inside a loop like that. >> >> What''s the database say after you do that? And what happens if you >> follow >> that up with a: >> >> product.reload >> >> I''d be really surprised if the database wasn''t adjusted after you did >> that, but can see that product wouldn''t know it''s been updated as >> ''p'' is >> local and not really tied to ''product''. >> >> -philip >> >>> > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
relations are always cached. the first time you call "object.relations" it goes and finds them all, but to save on SQL queries, it caches the objects. so next time you call "object.relations", there''s no query made. If instead you call "object.relations(true)", it''ll force that relation to be reloaded. If you call "object.reload" it''ll reload the whole object, and then if you call "object.relations" it''ll reload the relations again. I''d suggest class Product def destroy_prices self.prices.each do |p| p.destroy end self.prices(true) end end Josh Kieschnick wrote:> Yeah, sorry about this one. my product object must have been cached > inside of the console. > > It was deleting the objects from the database and when the console > was reloaded, the values were correct.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---