Hi there, How can I access and output the contents of the following array? <%= User.find(:all, :conditions => { ...whatever_conditions... }) %> When I try by simply adding a .join(", ") to the array, like so... <%= User.find(:all, :conditions => { ...whatever_conditions... }).join(", ") %> ...then I get a series of # signs: #, #, # How come? How can I access and display any attribute of "user"? Thanks a lot for your help! Tom -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Apr-04 16:19 UTC
Re: How to access / output the contents of an array?
On 4 Apr 2008, at 17:11, Tom Ha wrote:> > Hi there, > > How can I access and output the contents of the following array? > > <%= User.find(:all, :conditions => { ...whatever_conditions... }) %> > > When I try by simply adding a .join(", ") to the array, like so... > > <%= User.find(:all, :conditions => { ...whatever_conditions... > }).join(", ") %> > > ...then I get a series of # signs: #, #, # > > How come? >In rails 2.0, the default implementation of to_s generates something like #<User id: 1, name: "fred" > You''re just dumping that into the page, which isn''t valid html is confusing the hell out of the browser. Depending on what your trying to do, the debug helper is pretty good for inspecting stuff. Fred> How can I access and display any attribute of "user"? > > Thanks a lot for your help! > Tom > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Well, basically, I''d simply like to print some attribute (i.e. the nicknames) of all of the users in this array, i.e. separated by commas, like so: nickname1, nickname2, nickname3, ... Can you tell me what the correct code for that would be? Sorry, I haven''t come across the "debug helper" yet, how/where do I basically use that? Thanks a lot! -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Apr-04 16:40 UTC
Re: How to access / output the contents of an array?
On 4 Apr 2008, at 17:36, Tom Ha wrote:> > Well, basically, I''d simply like to print some attribute (i.e. the > nicknames) of all of the users in this array, i.e. separated by > commas, > like so: > > nickname1, nickname2, nickname3, ... > > Can you tell me what the correct code for that would be? > > Sorry, I haven''t come across the "debug helper" yet, how/where do I > basically use that?I wasn''t sure if you were trying to actually output something nicely or just dump some debug output to the screen (which the debug helper does nicely - just pass it an object and it will output some nice stuff). For what your''re doing, something like users.collect(&:name).join('', '') will do the trick (assuming users is an array of users and name is the attribute you care about) If you were outputting more stuff (eg a table row for each user, typically you''d use a partial. Fred --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Cool, thanks a lot, Fred! -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
In order to have access to all of the attributes, I would think of it this way: <% User.find(:all, :conditions => { ...whatever_conditions... }) .each do |user| %> <%= user.nickname %>, <%= user.another_attribute %> <% end %> ~or~ set an instance variable in the controller def index @users = User.find(:all, :conditions => { ...whatever_conditions... }) end then in the view (index.html.erb) <% @users .each do |user| %> <%= user.nickname %>, <%= user.another_attribute %> <% end %> --Ahab> How can I access and output the contents of the following array? > > <%= User.find(:all, :conditions => { ...whatever_conditions... }) %>--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---