title: ActiveRecord query through multiple joins
body: <p>I have a schema like this.</p>
  
  <p><img src="https://i.stack.imgur.com/70Yq9.png" alt="database schema"></p>
  
  <pre><code>managers  
      has_many :emails  
      has_many :stores
  
  emails  
      belongs_to :manager
  
  stores  
      belongs_to :manager  
      belongs_to :region
  
  regions  
      has_many :stores  
      has_many :readings
  
  readings  
      belongs_to :regions
  </code></pre>
  
  <p>I want to get readings for a manager. In SQL I would do something like this.</p>
  
  <pre><code>SELECT * FROM managers  
      JOIN stores ON stores.manager_id = managers.id  
      JOIN regions ON stores.region_id = regions.id  
      JOIN readings ON readings.region_number = regions.number  
  WHERE  
      manager.name = 'John Smith'  
  AND  
      regions.number = '1234567'
  LIMIT 100
  </code></pre>
  
  <p>I can't figure out how to do this in activerecord. I have been trying to make sense of <a href="http://guides.rubyonrails.org/active_record_querying.html" rel="noreferrer">http://guides.rubyonrails.org/active_record_querying.html</a> and <a href="http://guides.rubyonrails.org/association_basics.html" rel="noreferrer">http://guides.rubyonrails.org/association_basics.html</a> but it's not sinking in. I think I just need to see it from a different view point.</p>
  
  <p>I was thinking I would be accessing the data like this but I think I just don't understand how it works.</p>
  
  <pre><code>managers.name  
  managers.stores.name  
  managers.stores.regions.readings.limit(10)
  </code></pre>
  
  <p>I have been having to so something like this which is a whole lot uglier.</p>
  
  <pre><code>managers.first.stores.first.regions.first.readings.limit(10)
  </code></pre>