to wrap each instance variable of an ActiveRecord object between "<td>"
and "</td>", is there a way to "loop through" them, at least in the
debug mode, no matter there is getter methods to get them or not, or at
least the ones with the getter methods?
Such as
<% @stories.each do |s| %>
<%= "<tr>" %>
<% s.each_property do |i| %>
<%= "<td>#{h i}</td>\n" %>
<% end %>
<%= "</tr>\n" %>
<% end %>
to wrap each instance variable of an ActiveRecord object between "<td>"
and "</td>", is there a way to "loop through" them, at least in the
debug mode, no matter there is getter methods to get them or not, or at
least the ones with the getter methods?
Such as
<% @stories.each do |s| %>
<%= "<tr>" %>
<% s.each_property do |i| %>
<%= "<td>#{h i}</td>\n" %>
<% end %>
<%= "</tr>\n" %>
<% end %>
<%= "<table>\n" %>
<% row = 1 %>
<% @all_stories.each do |s| %>
<% if row == 1 %>
<%= "<tr>" %>
<% s.attributes.each do |i| %>
<% p i %>
<%= "<th>#{h i[0]}</th>\n" %>
<% end %>
<%= "</tr>\n" %>
<% end %>
<%= "<tr>" %>
<% s.attributes.each do |i| %>
<% p i %>
<%= "<td>#{h i[1]}</td>\n" %>
<% end %>
<%= "</tr>\n" %>
<% row += 1 %>
<% end %>
<%= "</table>\n" %>
it might be better if the internal loop counter can be used for looping
@all_stories so that now extra local variable "row" is needed.
I'm not sure that attributes() returns an ordered hash. That is, for each iteration you might get the fields in a different order. You should check that.
also, the use of i[0], i[1] seems a little less structured than if
i.attr_name, i.attr_value can be used.
look into "s.attributes.each_pair |k,v|"
the row == 1 situation also seems like somewhat not adhering to DRY.
You could consider using Story.column_names to print out the header and then using that to loop through and print out each object's attributes...