title: Rails 5 - Scope with latest has_many association
body: <p>I'm creating a filtered table for my user model. I've created a few scopes to filter them. I'm :</p>
  
  <pre><code>class User &lt; ApplicationRecord
    has_many :invoices
  
    scope :application_approved, -&gt; { ... }
    scope :application_denied, -&gt; { ... }
    scope :latest_invoice_paid, -&gt; { ... }
    scope :latest_invoice_not_paid, -&gt; { ... }
  
    def self.__self__
      self
    end
  end
  </code></pre>
  
  <p>and in the controller:</p>
  
  <pre><code>def index
    filters = params[:statuses] || {}
    application_status = filters[:application_status].presence
    payments_status = filters[:payments_status].presence
    @vehicles = Vehicle.send(application_status || :__self__)
                       .send(payment_status || :__self__)
                       .paginate(:page =&gt; params[:page], :per_page =&gt; 10)
                       .order('created_at DESC')
  end
  </code></pre>
  
  <p>All of the filters work in isolation, however when chained, filters that are not applied seem to cancel out the earlier filters. </p>
  
  <p>For example, if I set the filter to only show users who have paid, it works. But if I set the filter to only show users who have been approved/unapproved, all users are returned all the time. It seems as though returning <code>self</code> when a filter is not applied just returns all of the users.</p>
  
  <p>So, how can I skip the scope if a filter is not applied for it?</p>