title: activerecord find through association
body: <p>I am trying to retrieve an activerecord object from my db. My models are</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
     belongs_to :account
     has_many :domains, :through =&gt; :account    
  end
  </code></pre>
  
  <p>And</p>
  
  <pre><code>class Account &lt; ActiveRecord::Base
     has_many :domains
     has_many :users
  end
  </code></pre>
  
  <p>And</p>
  
  <pre><code>class Domain &lt; ActiveRecord::Base
     belongs_to :account
  end
  </code></pre>
  
  <p>Now I would like to retrieve a user based on the username and a domain name (lets assume that these are attributes of the User and the Domain classes respectively). i.e. something along the lines of</p>
  
  <pre><code>User.find(:first, :conditions =&gt;{:username =&gt; "Paul", :domains =&gt; { :name =&gt; "pauls-domain"}})
  </code></pre>
  
  <p>I know that the above piece of code will not work since I do have to mention something about the <em>domains</em> table. Also, the association between users and domains is a one-to-many (which probably further complicates things).</p>
  
  <p>Any ideas on how should this query be formed? </p>