I have a Rails 3 ActiveRecord that belongs_to two different ActiveRecords. Example
class Animal < ActiveRecord::Base
belongs_to: species
belongs_to: zoo
...
end
where the animals table contains a species_id, zoo_id, name, and description and the tables species with a scientific_name and zoo has address.
In the controller, I have a query
@animals = Animal.includes(:species, :zoo).order(:name)
and a list of columns I want displayed in the view,
@columns = ["name", "description", "species.scientific_name", "zoo.address"]
In the view, I want the creation of a HTML table to be driven by the list of column names, e.g.
<table>
<tbody>
<tr>
<% @animals.each do |animal| %>
<% %columns.each do |col| } %>
<td><%= animal[</td>
<% end %>
<% end %>
</tr>
</tbody>
</table>
this works great for animals's name and description, but does not work for species.scientific_name and zoo.address.
I know I could special case the loop and access the included classes directly like animal.species['scientific_name'], but I was hoping there would be a way to access the included classes by name. Something like animal['species']['scientific_name']