title: Rails 3.2 ActiveRecord Queries with Multiple Joins
body: <p>I have an issue trying to properly join table data and rendering it into a view.  I am currently designing a system so my restaurant clients can create dynamic menus and display them properly on the menu page. Currently I have a Menu, MenuItem, and Section models.  I have successfully created forms where I can assign menu_items to menus and associate a menu_item with a section.  My issue is rendering the front end menu.  I want my menu to look like the following template.  Any help would be greatly appreciated.</p>
  
  <pre><code>Menu Name
    First Section
      Menu Item
      Menu Item
      ...
    2nd Section
      Menu Item
      Menu Item
    ...
  </code></pre>
  
  <p>My models and associations are listed as follows.</p>
  
  <p>in Menu.rb</p>
  
  <pre><code>class Menu &lt; ActiveRecord::Base
    has_many :menu_menu_items, :dependent =&gt; :delete_all
    has_many :menu_items, :through =&gt; :menu_menu_items
  </code></pre>
  
  <p>in MenuItem.rb</p>
  
  <pre><code>class MenuItem &lt; ActiveRecord::Base
    has_many :menu_menu_items
    has_many :menus, :through =&gt; :menu_menu_items
    belongs_to :sections
  </code></pre>
  
  <p>in Section.rb</p>
  
  <pre><code>class Section &lt; ActiveRecord::Base
    has_many :menu_items,  :dependent =&gt; :nullify
  </code></pre>
  
  <p>So far in my view I have the following.  This is where I truly lack the understanding of how Rails and ActiveRecord work so I can properly create this view.  One thing I also need to know is that if a Section has no menu_items for a particular menu,  how do I exclude it?</p>
  
  <pre><code>&lt;div class="content"&gt;
  
    &lt;% @menus.each do |menu| %&gt;
      &lt;h3&gt;&lt;%= menu.name %&gt; &lt;br/&gt;&lt;/h3&gt;
      &lt;% Section.all.each do |section| %&gt;
        &lt;b&gt;&lt;%= section.name %&gt;&lt;br/&gt;&lt;/b&gt;
        &lt;% menu.menu_items.each do |menu_items| %&gt;
          &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;%= menu_items.name %&gt; &lt;br/&gt;
        &lt;% end %&gt;
      &lt;% end %&gt;
    &lt;% end %&gt;
  
  &lt;/div&gt;
  </code></pre>
  
  <p>&lt;--------------------------------------------------------- <strong>Edit</strong> ----------------------------------------------------------------></p>
  
  <p>I now have the following code working.  This gives me the sections and the corresponding menu items that only belong to the particular menu.  The only thing left that I need help on is the code still outputs an empty section if no menus have items in that section.  Any guidance?</p>
  
  <pre><code>&lt;% @menus.each do |menu| %&gt;
    &lt;h3&gt;&lt;%= menu.name %&gt; &lt;br/&gt;&lt;/h3&gt;
    &lt;% Section.all.each do |section| %&gt;
      &lt;b&gt;&lt;%= section.name %&gt;&lt;br/&gt;&lt;/b&gt;
      &lt;% menu.menu_items.where(:section_id =&gt; section.id).each do |menu_items| %&gt;
        &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;%= menu_items.name %&gt; &lt;br/&gt;
      &lt;% end %&gt;
    &lt;% end %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>&lt;---------------------------------------------------------<strong>Edit 2</strong>----------------------------------------------------------------></p>
  
  <p>I was able to derive the SQL I needed in order to pull the sections belonging to a particular menu.  How do I implement it?</p>
  
  <pre><code>select count(sections.id),sections.name
  from menu_items 
    join menu_menu_items on menu_items.id=menu_menu_items.menu_item_id
    join menus on menus.id=menu_menu_items.menu_id 
    join sections on menu_items.section_id= sections.id 
  where menu_id=16  #for example.
  group by sections.id;  
  </code></pre>