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 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
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 Hunter Hillegas wrote:> 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 > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hunter Hillegas
2007-Mar-08 01:21 UTC
Re: has_many :through - replace and clear Don''t Work?
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? On Mar 7, 2007, at 3:29 PM, Jon Garvin wrote:> > 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 > > > > Hunter Hillegas wrote: >> 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 >> >> >>> >> >> > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---