title: Create scope with .where on both collection and grand-parent
body: <p>I have three models with grand-parent, parent, child relation: <code>Organization</code>, <code>Category</code>, <code>Post</code>.</p>
  
  <p>I'm trying to create a scope in my <code>Post</code> model, using <code>where</code> first on the passed collection and then on the grand-parent:</p>
  
  <pre><code>scope :ready, -&gt; {
    where("next_setup_at &lt; ?", DateTime.current)
      .joins(category: :organization)
      .where("organizations.active = ?", true)
  }
  </code></pre>
  
  <p>But Postgres is throwing me an error:</p>
  
  <blockquote>
    <p>ActiveRecord::StatementInvalid: PG::AmbiguousColumn: ERROR:  column reference "next_setup_at" is ambiguous
    LINE 1: ...zations"."id" = "categories"."organization_id" WHERE (next_setup...
                                                                 ^</p>
    
    <p>: SELECT "posts".* FROM "posts" INNER JOIN "categories" ON "categories"."id" = "posts"."category_id" INNER JOIN "organizations" ON "organizations"."id" = "categories"."organization_id" WHERE (next_setup_at &lt; '2016-03-22 15:57:19.971887') AND (organizations.active = 't')</p>
  </blockquote>