# Comment.all returns array of what?

**URL:** <https://www.ruby-forum.com/t/comment-all-returns-array-of-what/202045>\
**Category:** Rails\
**Created:** [January 20, 2011, 3:05pm UTC](https://www.ruby-forum.com/t/comment-all-returns-array-of-what/202045 "2011-01-20T15:05:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [January 20, 2011, 3:05pm UTC](https://www.ruby-forum.com/t/comment-all-returns-array-of-what/202045/1 "2011-01-20T15:05:20Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![bourne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/22d042/32.png) [@bourne](https://www.ruby-forum.com/u/bourne)\
**Post date:** [January 20, 2011, 3:09pm UTC](https://www.ruby-forum.com/t/comment-all-returns-array-of-what/202045/2 "2011-01-20T15:09:38Z")

</div>

bourne wrote in post #976264:

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

## Best,

Marnen Laibow-Koser

> **[marnen.org: Homepage of Marnen Laibow-Koser](https://marnen.org/)**

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Sent from my iPhone

---

<div class="post-metadata">

**Author:** ![bourne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/22d042/32.png) [@bourne](https://www.ruby-forum.com/u/bourne)\
**Post date:** [January 20, 2011, 3:19pm UTC](https://www.ruby-forum.com/t/comment-all-returns-array-of-what/202045/3 "2011-01-20T15:19:11Z")

</div>

On Thu, Jan 20, 2011 at 2:04 PM, bourne [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

---

<div class="post-metadata">

**Author:** ![bourne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/22d042/32.png) [@bourne](https://www.ruby-forum.com/u/bourne)\
**Post date:** [January 20, 2011, 3:22pm UTC](https://www.ruby-forum.com/t/comment-all-returns-array-of-what/202045/4 "2011-01-20T15:22:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bourne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/22d042/32.png) [@bourne](https://www.ruby-forum.com/u/bourne)\
**Post date:** [January 20, 2011, 6:00pm UTC](https://www.ruby-forum.com/t/comment-all-returns-array-of-what/202045/5 "2011-01-20T18:00:06Z")

</div>

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

Marnen Laibow-Koser

> **[marnen.org: Homepage of Marnen Laibow-Koser](https://marnen.org/)**

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
