title: Rails displaying table data dynamically
body: <p>Models</p>
  
  <pre><code>class Smoothval &lt; ActiveRecord::Base
  
  has_many :smoothings
  has_many :steps
  
  end
  
  class Step &lt; ActiveRecord::Base
  belongs_to :usdzar
  belongs_to :smoothing
  belongs_to :smoothval
  
  default_scope lambda {
      order(id: :desc)
  }
  
  end
  
  class Usdzar &lt; ActiveRecord::Base
  
  has_many :smoothings
  has_many :steps
  
  end
  </code></pre>
  
  <p>The index view that I am trying to achieve:</p>
  
  <pre><code>&lt;table&gt;&lt;thead&gt;
  &lt;tr&gt;
  &lt;th&gt;Smoothing1&lt;/th&gt;
  &lt;th&gt;Smoothing2&lt;/th&gt;
  &lt;/tr&gt;&lt;/thead&gt;
  
  &lt;tbody&gt;
  &lt;tr&gt;
  &lt;td&gt;"finalsmprice","stepnum"&lt;/td&gt;
  &lt;td&gt;"finalsmprice","stepnum"&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/tbody&gt;
  &lt;/table
  </code></pre>
  
  <p>So all the fsmprices and the stepnums for every smoothval from the steps table</p>
  
  <p>My index.html.erb looks like this: </p>
  
  <pre><code>&lt;h1&gt;Listing Steps&lt;/h1&gt;
  &lt;table class="table table-striped table-bordered table-condensed"&gt;
  &lt;thead&gt;
  
  &lt;% Step.all.includes(:smoothval).group_by(&amp;:smoothval_id).each do |smoothval_id, steps| %&gt;
  &lt;tr&gt;
  &lt;th&gt;
  Smoothing: &lt;%= smoothval_id %&gt;
  &lt;/th&gt;
  &lt;/tr&gt;
  &lt;/thead&gt;
  
  &lt;tbody&gt;
  &lt;% steps.each do |step| %&gt;
   &lt;tr&gt;
   &lt;td&gt;
   (&lt;%= step.fsmprice %&gt;): &lt;%= step.stepnum %&gt;
    &lt;/td&gt;
  
  
    &lt;% end %&gt;
   &lt;/tr&gt; &lt;/tbody&gt;
  &lt;% end %&gt;
  
  &lt;/table&gt;
  </code></pre>
  
  <p>The table is very long and obviously displays all headings and data after one another. I have tried to create a postgres pivot table but that failed using tablefunc so back trying to do it in rails</p>
  
  <p>In my controller I can get the smoothvals (the table headings) into an array as follows, the array looks like [1, 5]: </p>
  
  <pre><code>sv = Smoothval.where('id IN (SELECT DISTINCT(steps.smoothval_id) FROM steps)')
  @assigned = sv.pluck(:id)     
  </code></pre>
  
  <p>How can I get the above table layout</p>
  
  <p>Thanks</p>