title: Access parent in scope
body: <p>I have a rails 3.2 application with an easy hierarchy: A user has many clients, a client has many invoices.</p>
  
  <p>I want the users to only see their own clients and invoices using scopes, by doing <code>Client.by_user(current_user)</code> and <code>Invoice.by_user(current_user)</code>. For clients, I have this, which works fine:</p>
  
  <pre><code>scope :by_user, lambda { |user| where(user_id: user.id) }
  </code></pre>
  
  <p>However, if I try the same for invoices</p>
  
  <pre><code>scope :by_user, lambda { |user| where(client.user_id =&gt; user.id) }
  </code></pre>
  
  <p>it fails, telling me <code>undefined local variable or method 'client'</code>.</p>
  
  <p>What am I doing wrong? I don't want to add user_ids to the invoices.</p>