The API docs for habtm don''t include mention of #build or #create as methods on the association. However, both methods exist in the HasAndBelongsToManyAssociation class. HABTM implements #build, while #create is inherited from AssociationCollection. #build seems to work fine, though there aren''t any tests for it. On the other hand, while #create will build and save the associated object, it doesn''t create a row in the join table. I think it''s an easy fix to get #create working, at least it looks like it. But since these methods aren''t documented I''m not sure what''s going on. Were they left out of the docs by accident, or was that intentional because of some subtle problem preventing a solid implementation? Is the broken code there a leftover that was meant to be removed, or an attempt that was meant to be finished later. If the answer is that #build and #create should be supported in habtm, I can open a ticket and see what I can do about fixing #create and getting some tests written. If the answer is they are leftover garbage, we should probably excise them so people aren''t confused by broken behavior. -- Josh Susser http://blog.hasmanythrough.com
> If the answer is that #build and #create should be supported in > habtm, I can open a ticket and see what I can do about fixing #create > and getting some tests written. If the answer is they are leftover > garbage, we should probably excise them so people aren''t confused by > broken behavior.They should indeed work. I think its just an omission born from the fact that you rarely do it this way. But it would be great to be able to. Please do make it work. Thanks! -- David Heinemeier Hansson http://www.loudthinking.com -- Broadcasting Brain http://www.basecamphq.com -- Online project management http://www.backpackit.com -- Personal information manager http://www.rubyonrails.com -- Web-application framework
On May 24, 2006, at 8:31 AM, David Heinemeier Hansson wrote:>> If the answer is that #build and #create should be supported in >> habtm, I can open a ticket and see what I can do about fixing #create >> and getting some tests written. If the answer is they are leftover >> garbage, we should probably excise them so people aren''t confused by >> broken behavior. > > They should indeed work. I think its just an omission born from the > fact that you rarely do it this way. But it would be great to be able > to. Please do make it work. Thanks!Found an existing ticket for the #create issue: http://dev.rubyonrails.org/ticket/3692 Off to write some tests... -- Josh Susser http://blog.hasmanythrough.com
David Heinemeier Hansson wrote:>> If the answer is that #build and #create should be supported in >> habtm, I can open a ticket and see what I can do about fixing #create >> and getting some tests written. If the answer is they are leftover >> garbage, we should probably excise them so people aren''t confused by >> broken behavior. > > They should indeed work. I think its just an omission born from the > fact that you rarely do it this way. But it would be great to be able > to. Please do make it work. Thanks!For completeness it''d be nice if the create! method was also added to each association type, so that one doesn''t have to code build(...).save!