HABTM Duplication

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:

The test case which is failing is:

Here’s where I think the issue may reside:

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!

On May 13, 10:05pm, Mark H [email protected] wrote:

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!

is it because your fixture files define two members that are active
(so they get added to the invoice too) ?

Fred

Hey Fred,

Nice catch, I went ahead and updated my fixtures file (so that no active
members are created). However I’m still running into the same issue :frowning:

https://github.com/mhayes/waterclub/commit/35e985ab5e09f6c40b576991838f901cc5e5247d

Hey Fred,

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:

https://github.com/mhayes/waterclub/commit/ef74f18a62dcfa65cb009f64f96c62a99115a569

So everything is working as expected now, just a little bit differently
than
I would’ve hoped. Thanks for the tips!

On May 13, 11:10pm, Mark H [email protected] wrote:

Hey Fred,

Nice catch, I went ahead and updated my fixtures file (so that no active
members are created). However I’m still running into the same issue :frowning:

https://github.com/mhayes/waterclub/commit/35e985ab5e09f6c40b57699183

So what are the members this newly created invoice has?

Fred

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs