How to access / output the contents of an array?

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

On 4 Apr 2008, at 17:11, Tom Ha wrote:

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

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!

On 4 Apr 2008, at 17:36, Tom Ha wrote:

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

Cool, thanks a lot, Fred!

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