I have a bad feeling asking this but I did not manage to find an answer
myself
@comments = Comment.all @comments is an array.
I can for example access comment.created_at directly, or I could do:
<% @comments.each do |comment| %>
<%= debug comment[âcreated_atâ] %>
<% end %>
I expected comment to be a hash, but comment.keys or comment.values
fail, as
well as an each_pair approach.
[I was actually trying to do a debug output printing all columns and
columns
names]
I have a bad feeling asking this but I did not manage to find an answer
myself
@comments = Comment.all @comments is an array.
I can for example access comment.created_at directly, or I could do:
<% @comments.each do |comment| %>
<%= debug comment[âcreated_atâ] %>
<% end %>
I expected comment to be a hash, but comment.keys or comment.values
fail, as
well as an each_pair approach.
[I was actually trying to do a debug output printing all columns and
columns
names]
Is Comment an ActiveRecord subclass? If so, then its instances arenât
hashes. Check out the docs for ActiveRecord::Base; the attributes
method may be particularly useful.
Thanks Marnen for the pointer to the attributes method!
For the archives:
<% @comments.each do |comment| %>
<% comment.attributes.each_pair do |key, value| %>
<%= key %>: <%= value %>
<% end %>
<% end %>
Youâre welcome! That looks like it will do what you want (but if youâre
not using Rails 3, remember to put in appropriate HTML escaping). Other
possibilities:
Just use comment.attributes.inspect
Instead of looping explicitly through comments, use render :partial,
:collection
Also, if you do this a lot (i.e. more than once or twice), build a
helper.
Oh, and consider using Haml instead of ERbâŚitâs a lot nicer.