title: Rails "where" clause for associations
body: <p>This seems like a simple question but it's a little puzzle to me:</p>
  
  <pre><code>class Parent
    has_many children
    ...
  end
  
  class Child
    belongs_to parent
  end
  
  p = Parent.find(111)
  c = Child.all.where(parent: p)
  </code></pre>
  
  <p>Why doesn't that work, and how come I have to do:</p>
  
  <pre><code>c = Child.all.where(parent_id: p.id)
  </code></pre>
  
  <h2>Addendum</h2>
  
  <p>A more complicated case has me creating a Relation based on more complicated logic, e.g.</p>
  
  <pre><code>c = Child.where(age: 32, city: "boston")
  c.where(parent: p) # wouldn't work
  </code></pre>
  
  <h2>Addendum #2</h2>
  
  <p>Wait I need to have a many to many to illustrate this:</p>
  
  <pre><code>class Teacher
     has_many :students, through: ClassRoom
     has_many :classes
  end
  
  class ClassRoom
    belongs_to :teacher
    belongs_to :child
  end
  
  class Child 
    has_many :classes
    has_many :teachers, through: ClassRoom
  end
  t = Teacher.first
  c = Child.where(age: 5, city: "boston")
  
  c.where(teacher: t) # wouldn't work
  c.where(teacher_id: t.id) # would work but is a little ugly
  </code></pre>
  
  <h2>Addendum 3</h2>
  
  <p>Thanks for all the great info! Is there a better (or 'correct') way to do the last line form the above example?</p>
  
  <pre><code>c.where(teacher_id: t.id) # would work but is a little ugly
  </code></pre>