Comment.all returns array of what?

I have a bad feeling asking this but I did not manage to find an answer myself :confused:

@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]

bourne wrote in post #976264:

I have a bad feeling asking this but I did not manage to find an answer
myself :confused:

@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.

Best,

I have a bad feeling asking this but I did not manage to find an answer myself :confused:

@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 tried doing what you are doing and It works. Try this in the console:

Comment.all.each { |comment| puts comment[“created_at”] }

Don’t know what debug does, but the above code works.

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 %>

Please quote when replying.

bourne wrote in post #976269:

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 %><br />
   <% 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.

Best,