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

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Joshua M. (Guest)
on 2006-01-31 17: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,
Joshua M. (Guest)
on 2006-01-31 17:44
OK, found the following:

But the reply I don't understand:

"Compound primary keys in your rel table."

What does that mean? Thanks.
Joshua M. (Guest)
on 2006-01-31 17: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.