Habtm: I can create the same relation more than 1x!


#1

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


#2

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.


#3

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?