# How to access / output the contents of an array?

**URL:** <https://www.ruby-forum.com/t/how-to-access-output-the-contents-of-an-array/133893>\
**Category:** Rails\
**Created:** [April 4, 2008, 6:11pm UTC](https://www.ruby-forum.com/t/how-to-access-output-the-contents-of-an-array/133893 "2008-04-04T18:11:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 4, 2008, 6:11pm UTC](https://www.ruby-forum.com/t/how-to-access-output-the-contents-of-an-array/133893/1 "2008-04-04T18:11:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 4, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/how-to-access-output-the-contents-of-an-array/133893/2 "2008-04-04T18:20:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 4, 2008, 6:36pm UTC](https://www.ruby-forum.com/t/how-to-access-output-the-contents-of-an-array/133893/3 "2008-04-04T18:36:10Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 4, 2008, 6:41pm UTC](https://www.ruby-forum.com/t/how-to-access-output-the-contents-of-an-array/133893/4 "2008-04-04T18:41:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 4, 2008, 7:06pm UTC](https://www.ruby-forum.com/t/how-to-access-output-the-contents-of-an-array/133893/5 "2008-04-04T19:06:48Z")

</div>

Cool, thanks a lot, Fred!

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 7, 2008, 3:21pm UTC](https://www.ruby-forum.com/t/how-to-access-output-the-contents-of-an-array/133893/6 "2008-04-07T15:21:53Z")

</div>

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
