title: How do I pass an argument to a has_many association scope in Rails 4?
body: <p>Rails 4 lets you scope a <code>has_many</code> relationship like so:</p>
  
  <pre><code>class Customer &lt; ActiveRecord::Base
    has_many :orders, -&gt; { where processed: true }
  end
  </code></pre>
  
  <p>So anytime you do <code>customer.orders</code> you only get processed orders.</p>
  
  <p>But what if I need to make the <code>where</code> condition dynamic?  How can I pass an argument to the scope lambda?</p>
  
  <p>For instance, I only want orders to show up for the account the customer is currently logged into in a multi-tenant environment.</p>
  
  <p>Here's what I've got:</p>
  
  <pre><code>class Customer &lt; ActiveRecord::Base
    has_many :orders, (account) { where(:account_id =&gt; account.id) }
  end
  </code></pre>
  
  <p>But how, in my controller or view, do I pass the right account?  With the code above in place when I do:</p>
  
  <pre><code>customers.orders
  </code></pre>
  
  <p>I get all orders for account with an id of 1, seemingly arbitrarily.</p>