Forum: Ruby on Rails Habtm: I can create the same relation more than 1x!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2006-01-31 16:18
Hi all

I have a DiscJockey and a Member model, each of them reference each
other using habtm.

josh $ script/server
>> m=Member.find 1
>> dj=DiscJockey.find 1
>> m.disc_jockeys << dj
>> m.disc_jockeys << dj
>> m.disc_jockeys << dj

Now I have 3 relations in the DB! Is this normal? Shouldn't there an
error occur, that I can only add the object a single time?
If it IS normal - how can I prevent it from adding it more than once?

Thanks a lot,
Josh
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2006-01-31 16:44
OK, found the following:

http://thread.gmane.org/gmane.comp.lang.ruby.rails/14842

But the reply I don't understand:

"Compound primary keys in your rel table."

What does that mean? Thanks.
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2006-01-31 16:51
I just remarked that the uniq option ignores duplicate relationships,
but you still can add as many duplicates as you wish. And:

>> m = Member.find 1
>> d = DiscJockey.find 1
>> m.disc_jockeys.push d
>> m.disc_jockeys.push d
>> m.disc_jockeys.size # 2
>> m.reload
>> m.disc_jockeys.size # 1

This can't be no bug, can it?
This topic is locked and can not be replied to.