title: Applying two scopes in one ActiveRecord query
body: <p>My <strong>Program</strong> model has this:</p>
  
  <pre><code>has_many :registry_patient_counts
  scope :salary_and_bonus, 
    :joins =&gt; :registry_patient_counts,
    :order =&gt; "patient_count_percentage DESC"
  </code></pre>
  
  <p>and this works well when I start simple an baby steps with my controller which is this:</p>
  
  <pre><code>programs = 
    Program.includes([:registry_patient_counts])
      .salary_and_bonus.where('organization_id = 1')
      .limit(2)
  </code></pre>
  
  <p>but my ultimate controller should look like this ( How it looks like without having scopes )</p>
  
  <pre><code>@programs = Program.includes(
    [:registry_patient_counts, measures: :measures_off_targets])
    .where('organization_id = 1')
    .limit(2)
  </code></pre>
  
  <p>so notice it has a  <code>measures: :measures_off_targets</code> extra in it. 
  So similarly between measures and measures_off_targets I want to define a similar scope as the previous one .... but then I don't understand how to type both of those scopes in the controller? </p>
  
  <hr>
  
  <p><strong>EDIT: So this is what we have so far by following the answer below:</strong></p>
  
  <p>In Program.rb:</p>
  
  <pre><code>scope :rpc,   includes(:registry_patient_counts).order =&gt; "patient_count_percentage DESC"
  
  scope :msr, includes(measures: :measures_off_targets)
  </code></pre>
  
  <p>And controller query looks like this:</p>
  
  <pre><code>@programs2 = Program.rpc.msr.where('organization_id: 1').limit(2)
  </code></pre>
  
  <p>where the error that RubyMine shows in the IDE is that "cannot find msr"
  If I ignore that and go to browser, still in the Rail console I get a "Error 500, bad request" and nothing back.</p>