title: How to get a unique set of parent models after querying on child
body: <p><code>Order</code> <code>has_many</code> <code>Items</code> is the relationship.</p>
  
  <p>So let's say I have something like the following 2 orders with items in the database:</p>
  
  <pre><code>Order1 {email: alpha@example.com, items_attributes:
      [{name: "apple"},
       {name: "peach"}]
  }
  Order2 {email: beta@example.com, items_attributes:
      [{name: "apple"},
       {name: "apple"}]
  }      
  </code></pre>
  
  <p>I'm running queries for Order based on child attributes. So let's say I want the emails of all the orders where they have an Item that's an apple. If I set up the query as so:</p>
  
  <pre><code>orders = Order.joins(:items).where(items: {name:"apple"})
  </code></pre>
  
  <p>Then the result, because it's pulling at the <code>Item</code> level, will be such that:</p>
  
  <pre><code>orders.count = 3
  orders.pluck(:email) = ["alpha@exmaple.com", "beta@example.com", "beta@example.com"]
  </code></pre>
  
  <p>But my desired outcome is actually to know what unique orders there are (I don't care that <code>beta@example.com</code> has 2 apples, only that they have at least 1), so something like: </p>
  
  <pre><code>orders.count = 2
  orders.pluck(:email) = ["alpha@exmaple.com", "beta@example.com"]
  </code></pre>
  
  <p>How do I do this?</p>
  
  <p>If I do <code>orders.select(:id).distinct</code>, this will fix the problem such that <code>orders.count == 2</code>, BUT this distorts the result (no longer creates AR objects), so that I can't iterate over it. So the below is fine</p>
  
  <pre><code>deduped_orders = orders.select(:id).distinct
  deduped_orders.count = 2
  deduped_orders.pluck(:email) = ["alpha@exmaple.com", "beta@example.com"]
  </code></pre>
  
  <p>But then the below does NOT work:</p>
  
  <pre><code>deduped_orders.each do |o|
    puts o.email # ActiveModel::MissingAttributeError: missing attribute: email
  end
  </code></pre>
  
  <p>Like I basically want the output of <code>orders</code>, but in a unique way. </p>