Lindsay Boyd
2006-Apr-29 18:28 UTC
[Rails] HABTM - how to insert join row when associated rows exist
I have successfully used HABTM to create a many-to-one-to-many set of rows in one step. Now, if two objects I want to associate already exist, how do I create the join row? The Agile book mentions the ''push_with_attributes'' method. The text says this method is useful for adding additional attributes to the join row (in the example a ''read_at'' timestamp). I only require a simple two-key join row - is ''push_with_attributes'' the way to go? -Lindsay -- Posted via http://www.ruby-forum.com/.
Lindsay Boyd
2006-Apr-29 18:30 UTC
[Rails] Re: HABTM - how to insert join row when associated rows exis
Lindsay Boyd wrote:> many-to-one-to-many set of rowsshould read '' one-to-many-to-one rows'' -Lindsay -- Posted via http://www.ruby-forum.com/.
Chris Hall
2006-Apr-29 19:36 UTC
[Rails] HABTM - how to insert join row when associated rows exist
push with attributes is not what you want to use. that''s for when you have additional columns in your join table other than the two id columns for the joined tables. example of what you want to do: assumiing a habtm between foo and bar (using table bars_foos as join tabled) foo = Foo.find(1) bar = Bar.find(1) foo.bars << bar # or you could do bar.foos << foo, same results this will create the association in the join table. http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000533 Chris On 4/29/06, Lindsay Boyd <lindsay.boyd@ntlworld.com> wrote:> > I have successfully used HABTM to create a many-to-one-to-many set of > rows in one step. Now, if two objects I want to associate already exist, > how do I create the join row? The Agile book mentions the > ''push_with_attributes'' method. The text says this method is useful for > adding additional attributes to the join row (in the example a ''read_at'' > timestamp). I only require a simple two-key join row - is > ''push_with_attributes'' the way to go? > > -Lindsay > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060429/d977b1d0/attachment.html
Lindsay Boyd
2006-Apr-29 20:03 UTC
[Rails] Re: HABTM - how to insert join row when associated rows exis
It''s OK, I figured it out. -Lindsay -- Posted via http://www.ruby-forum.com/.
Christer Nilsson
2006-Apr-30 09:43 UTC
[Rails] Re: HABTM - how to insert join row when associated rows exis
Lindsay Boyd wrote:> It''s OK, I figured it out. > > -LindsayPlease end your monolog in a proper way by stating your solution. -- Posted via http://www.ruby-forum.com/.
Lindsay Boyd
2006-Apr-30 13:54 UTC
[Rails] Re: HABTM - how to insert join row when associated rows exis
> Please end your monolog in a proper way by stating your solution.A bug in my model''s validation prevented the join row from being created. No solution as such. - Lindsay -- Posted via http://www.ruby-forum.com/.