# Moving belongs\_to object to different association

**URL:** <https://www.ruby-forum.com/t/moving-belongs-to-object-to-different-association/189874>\
**Category:** Rails\
**Created:** [June 9, 2010, 10:36am UTC](https://www.ruby-forum.com/t/moving-belongs-to-object-to-different-association/189874 "2010-06-09T10:36:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeffrey\_LSTaylor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ee7513/32.png) [@Jeffrey\_LSTaylor](https://www.ruby-forum.com/u/Jeffrey_LSTaylor)\
**Post date:** [June 9, 2010, 10:36am UTC](https://www.ruby-forum.com/t/moving-belongs-to-object-to-different-association/189874/1 "2010-06-09T10:36:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jeffrey\_LSTaylor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ee7513/32.png) [@Jeffrey\_LSTaylor](https://www.ruby-forum.com/u/Jeffrey_LSTaylor)\
**Post date:** [June 9, 2010, 10:44am UTC](https://www.ruby-forum.com/t/moving-belongs-to-object-to-different-association/189874/2 "2010-06-09T10:44:12Z")

</div>

On Jun 9, 9:34 am, “Jeffrey L. Taylor” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![Jeffrey\_LSTaylor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ee7513/32.png) [@Jeffrey\_LSTaylor](https://www.ruby-forum.com/u/Jeffrey_LSTaylor)\
**Post date:** [June 9, 2010, 4:20pm UTC](https://www.ruby-forum.com/t/moving-belongs-to-object-to-different-association/189874/3 "2010-06-09T16:20:15Z")

</div>

Quoting Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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
