I’m new to rails and databases, needless to say I’m pretty confused
here. There’s this issue I don’t understand how to resolve, heck I
don’t even know if it’s an issue. I’ve also been searching for answers
here, forums, irc, and nothing. Anyway I’ll try to be as clear as I
can be. And I apologize for the many questions in one thread.
Let’s say I have two tables, girls and boys (to spicy up this topic),
with their respective models, which in turn have a
has_and_belongs_to_many relationship. For that relationship to work, I
have a join table called boys_girls, with two columns, boy_id and
girl_id.
in rails, I create a boy called brad, and a girl called angelina.
now if I do:
brad.girls << angelina
brad.girls << angelina
not only will angelina be two times in his array (if only this stuff
could be in real life), but that relationship will appear in two rows
on the join table.
First question: as far as database performance and size goes, is this
a problem?
Anyway, if I add uniq => true to has_and_belongs_to_many on the
models, ActiveRecord will successfully ignore this duplication when I
reload the objects. But it will still act the same way as I said
before: if I add duplication, it will show up in the existing array
and it will be added to the table.
So, my second question is, how do I avoid this duplication?
I found in the agile web dev book that I can add an index to the join
table right in the migration, and add :unique => true after the
add_index call. I have tried it, and no difference. I suppose it only
configures the index to ignore duplicates, which would then resolve
performance issues?
Also, I have read that validates_uniqueness_of accepts various columns
with scope, but I’m not sure how to do that, and also, am I right to
say that scope only helps to limit uniqueness to given sets of rows?
In that case that wouldn’t help, right?
And, in case validation in the model is the way to go, where should I
put it? In the Girl and Boy models? or should I create a model to
represent the join table rows and do that validation there?
(Boys and girls might not exactly illustrate what I need. In my case,
I really don’t need to add more information to the join table, so
validation would possibly be the only reason to create a third model.)