# Has\_many :through - replace and clear Don't Work?

**URL:** <https://www.ruby-forum.com/t/has-many-through-replace-and-clear-dont-work/102707>\
**Category:** Rails\
**Created:** [March 7, 2007, 11:52pm UTC](https://www.ruby-forum.com/t/has-many-through-replace-and-clear-dont-work/102707 "2007-03-07T23:52:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hunter\_h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bc79bd/32.png) [@hunter\_h](https://www.ruby-forum.com/u/hunter_h)\
**Post date:** [March 7, 2007, 11:52pm UTC](https://www.ruby-forum.com/t/has-many-through-replace-and-clear-dont-work/102707/1 "2007-03-07T23:52:22Z")

</div>

I have the following setup:

class NewsItem \< ActiveRecord::Base  
has\_many :connections, :as =\> :connectable, :dependent =\> :destroy  
has\_many :ideas, :through =\> :connections  
end

The relationship is a has\_many :through that is also polymorphic.

I can add ideas to news items with this code:

@news\_item.ideas.push(related\_ideas)

related\_ideas is an array of ideas.

The problem comes when I try to update the ideas for an existing news  
item.

First I tried this:

@news\_item.ideas.replace(new\_ideas)

This literally does nothing - no log output at all, no DB activity.

Then I tried this:

@news\_item.ideas.clear  
@news\_item.ideas.push(new\_ideas)

Again, nothing at all. No error, nada. I would have expected the  
‘clear’ method call to remove the records that associate the news  
items and the ideas.

What am I doing wrong?

Thanks,  
Hunter

---

<div class="post-metadata">

**Author:** ![hunter\_h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bc79bd/32.png) [@hunter\_h](https://www.ruby-forum.com/u/hunter_h)\
**Post date:** [March 8, 2007, 12:30am UTC](https://www.ruby-forum.com/t/has-many-through-replace-and-clear-dont-work/102707/2 "2007-03-08T00:30:26Z")

</div>

Not sure why what you’re trying doesn’t work, but as a workaround until  
that gets figured out, how about…

@news\_item.ideas = new\_ideas

---

<div class="post-metadata">

**Author:** ![hunter\_h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bc79bd/32.png) [@hunter\_h](https://www.ruby-forum.com/u/hunter_h)\
**Post date:** [March 8, 2007, 2:22am UTC](https://www.ruby-forum.com/t/has-many-through-replace-and-clear-dont-work/102707/3 "2007-03-08T02:22:42Z")

</div>

Doing that gives me a no such method error:

undefined method `ideas=’ for #NewsItem:0x31724bc

Can relationships be accessed in that manner?

I know that I could do:

@news\_item.ideas \<\< new\_ideas

But that won’t clear the old ones, just add new ones…

Any other ideas?
