Search code examples
ruby-on-railssingle-table-inheritancesti

Include Rails STI parent with children


I have two types using a STI Model - Single Table Inheritance

class Entry < ActiveRecord::Base
  belongs_to :cycle 

class Cycle < Entry
  has_many :entries

In the cycle/show.html.erb I have a typical table that displays all the entries belonging to this cycle.

<% @cycle.entries.each do |entry| %>
<table>display rows of data from entries</table>
<% end %>

Is there a clean way I can include the parent Cycle data in the first row of the table without repeating the markup with @cycle.value


Solution

  • Right, so put the "displays rows of data" into a partial called, say '_table_lines' where you refer to the model as "object"

    (e.g.

    <ul>
    <li><%= object.description %></li> 
    <li><$= object.created_at %></li>
    </ul> 
    

    )

    Then

    <%= render partial: 'table_lines', locals: {object: @cycle} %>
    <% @cycle.entries.each do |entry| %>
    <%= render partial: 'table_lines', locals: {object: entry} %>
    <% end %>