title: Rails loop with heading
body: <p>I have this loop in Rails and I want to list each associated <code>region_id</code> under each <code>region.name</code>. So far under each <code>region.name</code> I get a list of all items.</p>
  
  <pre><code>Region has_many :trials
  Trials belongs_to :region, :primary_key =&gt; 'region_id'
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>  &lt;% @regions.each do |region, list| %&gt;
          &lt;h3&gt;&lt;%= region.region.name %&gt;&lt;/h3&gt;
          &lt;table class="table"&gt;
          &lt;% list.each do |list| %&gt;
              &lt;tr&gt;
              &lt;td&gt;
            &lt;%= link_to list.site.site_name, trial_trials_path(trial_id: list.trial_id) unless list.site.site_name.blank? %&gt;
              &lt;/td&gt;
              &lt;td&gt;    
              &lt;%= link_to list.trial_type, trial_trials_path(trial_id: list.trial_id) unless list.trial_type.blank? %&gt;
              &lt;/td&gt;
              &lt;td&gt;    
              &lt;%= link_to list.grower.name, trial_trials_path(trial_id: list.trial_id) unless list.grower.name.blank? %&gt;
              &lt;/td&gt;
              &lt;/tr&gt;
          &lt;% end %&gt;
          &lt;/table&gt;
      &lt;% end %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code> def index
   list = Trial.where('year = ?', Time.now.year).order(:region_id)
   @regions = list.group_by { |t| t.region_id }
   end
  </code></pre>