Forum: Ruby on Rails HABTM Duplicates?

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.
A08294389f09fd2f509ff027540ddd75?d=identicon&s=25 unknown (Guest)
on 2006-06-11 17:12
(Received via mailing list)
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.
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-06-11 20:01
> 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 Susser
http://blog.hasmanythrough.com
This topic is locked and can not be replied to.