Hi there,
my problem is kind of difficult to explain but maybe simple to answer: I
have a model (book) with author:string, title:string, id:integer and so
on.
If I do a query @book = Book.find(:id => 123) the result is an instance
of book.
Now I want to iterate through the key/value pairs of this instance, but
as it is not a hash methods like "each" don''t work.
Example:
<% @book.each do |k,v|%>
<tr>
<td><%= k %></td>
<td><%= v %></td>
</tr>
<% end %>
Can anyone please point me to the right direction?
Thanks and greetings
Sven
--
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.
I forgot to mention that my model does not inherit from activeRecord. It''s a MongoMapper::Document... -- 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.
On Sun, Jul 18, 2010 at 6:54 AM, Sven Koesling <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Now I want to iterate through the key/value pairs of this instance, but > as it is not a hash methods like "each" don''t work.> <% @book.each do |k,v|%>@book.attributes.each do .... -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan -- 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.
> > @book.attributes.each do .... >Thanks a lot, Hassan, that was it! S. -- 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.
Hi Sven, On Sun, Jul 18, 2010 at 8:54 AM, Sven Koesling <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi there, > > my problem is kind of difficult to explain but maybe simple to answer: I > have a model (book) with author:string, title:string, id:integer and so > on. > If I do a query @book = Book.find(:id => 123) the result is an instance > of book. > Now I want to iterate through the key/value pairs of this instance, but > as it is not a hash methods like "each" don''t work. > > Example: > > <% @book.each do |k,v|%> > <tr> > <td><%= k %></td> > <td><%= v %></td> > </tr> > <% end %> > > Can anyone please point me to the right direction?I''m not sure why you want to do this as it will be much less readable than explicitly enumerating the fields. Having said that, I''d recommend you take a look through the public methods for your instance variable (I''ve never used MongoDB). There''s probably an equivalent to ''attribute_names'' which returns an array of all column names in an AR model. If you were using AR you could iterate through the array for the ''keys'' and use @book[''k''] for the values. HTH, Bill -- 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.