I have two AR models in a one-to-many relationship. class Feed < ActiveRecord::Base belongs_to :channel, :counter_cache => true end class Channel has_many :feeds end I need to move a feed to a different channel. This works, except the feeds_counter cache is not changed for the ''to'' channel. What do I need to do differently? old_channel = ... new_channel = ... f = old_channel.feeds[0] old_channel.feeds.delete(f) new_channel.feeds << f Initially, there is one feed in each channel. old_channel.feeds_count is zero (Yeah!), but new_channel.feeds_count is still one (Boo!). What to do differently? TIA, Jeffrey -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Jun-09 08:43 UTC
Re: Moving belongs_to object to different association
On Jun 9, 9:34 am, "Jeffrey L. Taylor" <r...-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org> wrote:> > Initially, there is one feed in each channel. old_channel.feeds_count is zero > (Yeah!), but new_channel.feeds_count is still one (Boo!). >does feed.channel = new_channel work better ? Fred -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jeffrey L. Taylor
2010-Jun-09 14:18 UTC
Re: Re: Moving belongs_to object to different association
Quoting Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > > On Jun 9, 9:34 am, "Jeffrey L. Taylor" <r...-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org> wrote: > > > > Initially, there is one feed in each channel. old_channel.feeds_count is zero > > (Yeah!), but new_channel.feeds_count is still one (Boo!). > > > > does > > feed.channel = new_channel > > work better ? > > Fred >Fred, Duh, smacks forehead. It is certainly simpler. Yes it does work. Thank you very much. Too much seeing the single tree blocking my path and missing the paved road right next to it. Thank you, Jeffrey -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.