title: Why does ActiveRecord query return ambiguous column reference?
body: <p>In a Rails app I'm setting up nested routes</p>
  
  <pre><code>resource Country do
    resource State
    resource City
    resource User
    resource Post
  end
  </code></pre>
  
  <p>I now want to display users that have posted within a country at <code>../country/1/users</code></p>
  
  <p>In the User controller I have:</p>
  
  <pre><code>def index 
    @user = User.includes(:posts, :city, :state, :country)
    @user = @users.where("states.country_id = ?", params[:country_id]) if params[:country_id]
    @active_users = @users.where('posts_count &gt; ?', 0).ranked_scope
  end
  </code></pre>
  
  <p>I'm, getting:</p>
  
  <pre><code>PG::Error: ERROR:  column reference "posts_count" is ambiguous
  LINE 1: ...untry_id" WHERE (states.country_id = '1') AND (posts_co...
  </code></pre>
  
  <p>I'm not sure if this is because:</p>
  
  <ol>
  <li>There is a <code>posts_count</code> column on User, City, State, Country models and the query does not know which on to use. If so, what is the correct syntax to specify the table?</li>
  <li>I'm stringing together two where clauses incorrectly.</li>
  <li>Something else....?</li>
  </ol>
  
  <p>I'd appreciate any suggestions or ideas.</p>