Forum: Ruby on Rails has_many :through - replace and clear Don't Work?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
A7db9ec803b5895ae5f916a74e2db329?d=identicon&s=25 Hunter Hillegas (Guest)
on 2007-03-07 23:52
(Received via mailing list)
I have the following setup:

class NewsItem < ActiveRecord::Base
   has_many :connections, :as => :connectable, :dependent => :destroy
   has_many :ideas, :through => :connections

The relationship is a has_many :through that is also polymorphic.

I can add ideas to news items with this code:


related_ideas is an array of ideas.

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

First I tried this:


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

Then I tried this:


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?

3299251223f55753ec84719de39e6e53?d=identicon&s=25 Jon Garvin (Guest)
on 2007-03-08 00:30
(Received via mailing list)
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
A7db9ec803b5895ae5f916a74e2db329?d=identicon&s=25 Hunter Hillegas (Guest)
on 2007-03-08 02:22
(Received via mailing list)
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?
This topic is locked and can not be replied to.