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] -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org Sent from my iPhone -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Thu, Jan 20, 2011 at 2:04 PM, bourne <bourne7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 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.> 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] > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Apparently Analagous Threads
- Rails 3 Active Record query returns "undefined method `loaded?' for #<Array:0x126a4c>"
- to "loop through" the instance variables of ActiveRecord?
- Better way to count Active Record Data?
- rake db:seed with has_many through (m:n with seperate table)
- Mysql query optimization for order condition