andrewdmason-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Feb-28 22:27 UTC
create on a has_many through?
Hi, I have two models, User and Item, as well as a join model, UserItem. So the User model includes the lines: has_many :user_items has_many :items, :through => :user_items I''ve noticed that this doesn''t work: u = User.new u.items.create :name => "hammer" Any reason why? Seems like it would be a nice feature. To get around it, I usually do this: u.items << Item.create :name => "hammer" or u.user_items.create(:item => User.create(:name => "hammer")) Thanks for your time, Andrew --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
andrewdmason-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Hi, I have two models, User and Item, as well as a join model, > UserItem. So the User model includes the lines: > > has_many :user_items > has_many :items, :through => :user_items > > I''ve noticed that this doesn''t work: > > u = User.new > u.items.create :name => "hammer" > > Any reason why? Seems like it would be a nice feature. To get around > it, I usually do this: > > u.items << Item.create :name => "hammer" > or > u.user_items.create(:item => User.create(:name => "hammer"))You can''t do that with create, but create! will do what you want. -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
The reason this does not work is because your user object does not exist in the database yet and there is no id to create the association. either create or save the user first. see the docs on why http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000642 On 2/28/07, andrewdmason-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <andrewdmason-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi, I have two models, User and Item, as well as a join model, > UserItem. So the User model includes the lines: > > has_many :user_items > has_many :items, :through => :user_items > > > I''ve noticed that this doesn''t work: > > u = User.new > u.items.create :name => "hammer" > > Any reason why? Seems like it would be a nice feature. To get around > it, I usually do this: > > u.items << Item.create :name => "hammer" > or > u.user_items.create(:item => User.create(:name => "hammer")) > > > > > Thanks for your time, > > Andrew > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---