title: How could I query these 3 associated models?
body: <p>Imagine these models:</p>
  
  <pre><code>class User
    belongs_to :profile
    # has email here
  end
  
  class Profile
    has_one :user
    # has first_name,last_name
  end
  </code></pre>
  
  <p>and</p>
  
  <pre><code>class Post
    belongs_to :profile
    # has title,content
  end
  </code></pre>
  
  <p>Now, I would like to query all posts ( do a LIKE "%substring%" ) on the user's email. I would prefer to not have to write it with map/selects as I think it would generate pretty inefficient code. I tried something like that:</p>
  
  <pre><code>class Post
    def self.with_user_email_like(email)
      self.joins(:profile).where("profile.email LIKE ?","%#{email}%")
    end
  end
  </code></pre>
  
  <p>The thing is, I know somehow I should have a profile.user.email in the condition above, but I just can't get it to work. Any suggestions?</p>