HABTM Duplicates?


#1

Hello all,

I’m working on a small Rails site for a game I play that will
be used to add new characters. Part of this addition process is choosing
what
skills the character will have. For this, in MySQL, I have a characters
table
and a skills table, as well as a characters_skills table that is used as
the
join table in the habtm relationship between the two. I am presenting
the
skills in select boxes, which are separated based on the category of the
skill.

Everything is working well, except for one issue. Due to the nature of
this game, skills can be selected multiple times. However, when Rails
inserts
goes to insert the selected data, it skips the duplicates, e.g. if the
selected
ids are 1, 1, 2, 3, only 1, 2, and 3 will be written into the join
table.

Is there perhaps an option to have Rails write duplicates into the
table,
or am I looking at the whole model incorrectly? Any advice would be
appreciated.


#2

Is there perhaps an option to have Rails write duplicates into the
table,
or am I looking at the whole model incorrectly? Any advice would be
appreciated.

That should work. Hard to say why it’s not without seeing your code. But
another approach would be to use a has_many :through association with a
count attribute in the join model. Then you wouldn’t need to worry about
counting multiple rows in the join table.


Josh S.
http://blog.hasmanythrough.com