0

I have added a multiple select box like this one to my form HTML multiple select box.

I have a User object and a Personality object. A user can have 0 or more personalities and a personality can "belong" to 0 or more users. For this many to many relation I created a third model UserPersonality to store the ids of the User and Personality.

My User and Personality models have their corresponding has_many through indications. My question is, how can I make the helper add the association to the UserPersonality table through a select box like the one on the left from the link provided? If a user adds a personality from the right select box to the left box I want to add a record such as:

UserId  PersonalityId
1         3

To connect a User with the Personality that is selected.

Thanks.

Community
  • 1
  • 1
bb2
  • 2,872
  • 7
  • 37
  • 45

1 Answers1

0

It was easier to solve the problem using the has_and_belongs_to_many association vs the has_many through

This is because I'm only using the join table for the ids and with this association I can simply do

u=User.find(1)
p=Personality.find(1)
u.personalities << p

To associate them and populate the joint table: users_personalities

bb2
  • 2,872
  • 7
  • 37
  • 45