1

See the details below to make sense.

I am trying to write a line of code that will be searching the database and show the users that have a specific skill.

My clue is that I should be using something like @user.where('skill = ?', skillvariable) or a specific query. Any point/guidance to the right direction is greatly appreciated.

Details:

I have a users model, a skills model and a user_skills model.

In user.rb I have the relation has_many :skills, through: :user_skills.

In the skill.rb I have the relations has_many :user_skills and has_many :users, through: :user_skills,

and then in user_skills.rb I have the belongs_to :user and belongs_to :skill.

The user_skills schema is:

  t.integer "user_id"
  t.integer "skill_id"

And the skills schema is:

  t.string   "name"
  t.string   "slug"

1 Answers1

1

You can simply do:

User.includes(:skills).where(skills: { name: 'Archery' })

This will letterally:

Retrieve all users having at least one skill named 'archery'.


Similar questions:

Community
  • 1
  • 1
MrYoshiji
  • 54,334
  • 13
  • 124
  • 117