0

This question seems to have been asked many times before, but I didn't see it satisfactorily answered anywhere.

I have three tables/models:

skill
----------------------------------
id
label


user
----------------------------------
id
username
(many other irrelevant attributes)


user_skill
----------------------------------
id
user_id
skill_id
current_proficiency_level
interest_level

So each user has any number of skills, and each user can indicate how proficient he is with that skill right now (maybe I'm a 2 with PHP) and then what your interest level is (but maybe I want to learn PHP, so my interest level is an 8).

I'm wondering what the simplest, "most default" way of saving these UserSkill records is.

Let's not worry right now about the format in which my User object is getting the UserSkill records. Let's just assume that I have some user and I want to save, say, 10 UserSkill records when I save my User object. How would I do that?

(By the way, I'm reasonably well-versed with Rails in general, I just haven't bothered yet to figure out the "right" way to do this kind of save. So if you reference other Rails terms/practices, I'll probably know what you're talking about.)

Jason Swett
  • 43,526
  • 67
  • 220
  • 351

2 Answers2

0

Usually, has_many :through is used if the join table is going to be manipulated.

See here, for example: http://guides.rubyonrails.org/association_basics.html#choosing-between-has_many-through-and-has_and_belongs_to_many

In either case, you would add Skills to a User the same way: @user.skills << a_skill_object or @user.skills = array_of_skill_objects (this would remove any skills already associated with the @user).

The attributes on the join table can be set manually after the association is made: @user.user_skills.first.current_proficiency_level = 42

jordanpg
  • 6,386
  • 4
  • 46
  • 70
0

I ended up doing @user.user_skills = (values).

Jason Swett
  • 43,526
  • 67
  • 220
  • 351