title: ActiveRecord fetching records from has_many and related relationship through scopes
body: <p>I have two models: <code>Customer</code> and <code>Order</code>. Sometimes it happens that two Customers are actually the same customer, which is indicated by one of them having a <code>master_id</code> which points to another Customer record. When fetching Orders for a customer, I'd also like to fetch Orders from slave Customers (i.e. the ones that claim the Customer I'm querying from is their master Customer record).</p>
  
  <p>So:</p>
  
  <pre><code>Customer1: id: 1, master_id: nil, orders: [Order1, Order2]
  
  Customer2: id: 2, master_id: 1, orders: [Order3]
  </code></pre>
  
  <p>query <code>Customer1.all_orders</code> should return all 3 <code>Order</code> objects</p>
  
  <p>I know how to do this through a method - pluck ids of slave Customers, add the original customer's id to the array and then look for Orders whose customer_id is in this array:</p>
  
  <pre><code>has_many :slave_records, class_name: 'Customer', foreign_key: 'master_id'
  
  def all_orders
    order_ids = slave_records.map(&amp;:id).push(id)
    Order.where(customer_id: order_ids)
  end
  </code></pre>
  
  <p>But is there a way to do this using scopes and relations? It's Rails 4 (I know that Rails 5 was supposed to have an OR thing in its version of ActiveRecord).</p>