title: Rails query with condition in count
body: <p>I'm having a little trouble with a query in Rails.</p>
  <p>Actually my problem is:</p>
  <blockquote>
  <p>I want to select all <code>users</code> which do not have any <code>user_plans</code> <strong>AND</strong> his <code>role.name</code> is equals to <em>default</em>... <strong>OR</strong> has <code>user_plans</code> and all <code>user_plans.expire_date</code> are lower than <em>today</em></p>
  </blockquote>
  <p><code>user</code> has_many <code>roles</code></p>
  <p><code>user</code> has_many <code>user_plans</code></p>
  <pre><code>users = User.where(gym_id: current_user.id).order(:id)
  @users = []
  for u in users
    if u.roles.pluck(:name).include?('default')
      add = true
      for up in u.user_plans
        if up.end_date &gt; DateTime.now.to_date
          add = false
        end
      end
      if add
        @users &lt;&lt; u
      end
    end
  end
  </code></pre>
  <p>This code up here, is doing exactly what I need, but with multiple queries.
  I don't know how to build this in just one query.</p>
  <blockquote>
  <p>I was wondering if it is possible to do something like</p>
  <p><code>where COUNT(user_plans.expire_date &lt; TODAY) == 0</code></p>
  </blockquote>