title: Ruby on Rails: Duplicates with Join
body: <p>I am working on part of an app that will sort a list of children based on their parent's other children's attributes.</p>
  
  <p>Here are the classes I'm working with:</p>
  
  <pre><code>class SpecialChild
    belongs_to: Parent
  
  class Child_A
    belongs_to: Parent
  
  class Child_B
    belongs_to: Parent
  
  class Parent
    has_many: SpecialChild
    has_many: Child_A
    has_one: Child_B
  </code></pre>
  
  <p>These are the two order functions that are applied to it:</p>
  
  <pre><code>scope :order_child_a,
    joins("INNER JOIN child_a ON specialchild.parent_id = child_a.parent_id").
    where("booleanvalue = true")
  
  scope :order_parent_and_child_b,
    joins("LEFT OUTER JOIN parent ON specialchild.parent_id = parent.id").
    joins("LEFT OUTER JOIN child_b AS name ON parent.child_b_id = child_b.id").
    order("name ASC, parent.lastname ASC")
  </code></pre>
  
  <p>My problem is that even though there is only one SpecialChild in this list yet its Parent has multiple Child_A's that have booleanvalue = true, so I get copies of the same SpecialChild showing up even if it doesn't exist.</p>
  
  <p>EDIT:
  The problem arises in the first scope though I included the second one because I cannot do a .uniq without hitting more errors because of missing expected information. I also would like to avoid modifying order_parent_and_child_b as it is used throughout the entire app.</p>