I'm creating a web application similar to LinkedIn using Ruby on Rails (rails 4), and I'm trying to add a "skills" column in the User table. Each user can have multiple skills, I want to be able to categorize users by skills.
I could either have the "skills" column be of type array (a string array, to be exact), or I could have a new model for skills and make associations (using has_many). Which would be better, or is there a nicer solution?
Thanks!
You should definitely go with the Model-based Skill solution instead of a serialized Array or any other solution:
The global relation config would be like :
class User < ActiveRecord::Base
has_many :users_skills
has_many :skills, through: :users_skills
class UsersSkill < ActiveRecord::Base
belongs_to :user
belongs_to :skill
validates :user_id, :skill_id, presence: true
class Skill < ActiveRecord::Base
has_many :users_skills
has_many :users, through: :users_skills
With this, very easy to find Users from Skill, and vice-versa:
web_developpers = Skill.where(name: 'Web developper').first.users
johns_skills = User.where(name: 'John').first.skills
What if tomorrow your boss want you to add a feature like "preferred skill" or "best skill"? Or just a way to "order" (set a hierarchy between) a User's skills?
Just add a boolean column best on the join table (users_skills)
User.first.skills.where(users_skills: { best: true })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With