I’m experiencing an issue with duplicate entries being added when using has_and_belongs_to_many in my models. I’ve setup the following test Rails project (v3.0.7) here:
I noticed there were threads in the past with a HABTM duplication error, so I’m not sure if this is a regression of if I’m doing something wrong. Any ideas? Please let me know if I should provide any additional information, thanks!
When attempting to save the members with this newly created invice the HABTM relation was trying to add the same member (member_id=1 in my case) twice. However I setup a unique index on the composite key (member_id, invoice_id) so I end up with a SQLite3 ConstraintError being thrown (which I expected).
On another note I just changed my code to include a Participation model that I’m using to support the many-to-many relationship between Member and Invoice. Using has_many :members, :through => :participations works without trying to create duplicate Member entries. So I’m not sure if this is an issue with HABTM or my code. Related changes can be found here: