title: Doing search queries on many to many relationships
body: <p>Can I share my headches with you? I'm trying to do a search with this piece of code. (Line with arrow)</p>
  
  <pre><code>def index
      search = params[:user][:subject_ids]
  ----&gt;   @users = User.includes(:subjects).where(role: 'instructor')
      @user = current_user
  end
  </code></pre>
  
  <p>It's a many to many association. I have users, enrollements and subjects. I have instructors who have signed up to teach 1-5 courses. When I do the search, I get list of all the instructors, not just the ones teaching the subject I queried for. </p>
  
  <p>Not sure where to go from here. </p>
  
  <p>Associations </p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :enrollments, dependent: :destroy
    has_many :subjects, through: :enrollments
  end
  </code></pre>
  
  <hr>
  
  <pre><code>class Subject &lt; ActiveRecord::Base
  
    has_many :enrollments, dependent: :destroy
    has_many :users, through: :enrollments
  
  end
  </code></pre>
  
  <hr>
  
  <pre><code>class Enrollment &lt; ActiveRecord::Base 
  
    belongs_to :user
    belongs_to :subject
  
  end
  </code></pre>
  
  <p>Thank you for your time.</p>