0
board.voters_who_voted

returns an array containing rows from the User table. The attribute I want to pull out is the name.

Example output (only one user here):

[#<User id: 71, name: "montgomery Thamavaranacupt", email: "[email protected]", created_at: "2013-01-27 05:32:30", updated_at: "2013-04-24 07:07:43", password_digest: "$2a$10$Es/olEq0.w6vcnn4.8v0.O0VXDP1c7nktcW85UFtG91e...", remember_token: "ttPm1aTE6WBm3WVx4EJTew", admin: false, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil, avatar: 0>]

I suppose I need to iterate through the entire array and print the names.

This is what I tried:

board.voters_who_voted.each {|x| puts x.name}

however this just returns the entire array for some reason

board.voters_who_voted[0].name

the above properly returns the name of the first user but i need this for all of them

1 Answer 1

2

You can use map:

board.voters_who_voted.map(&:name)

Since you want to use this in the title attribute, I recommend the following:

<%= link_to .., .., :title => board.voters_who_voted.map(&:name).join("\n").html_safe %>
Sign up to request clarification or add additional context in comments.

8 Comments

This works, thanks! quick search indicates map and collect work the same way
@ayounesi, yeah they are actually aliases of each other.
I'm actually doing this for a tooltip so it's in the title element of a link_to. Not sure how to put the <br/> tag within the quotes title: "#{board.voters_who_voted.collect(&:name).join("\n")}" what i had. \n still does nothing
still no dice but Ill tinker with it some more
@ayounesi If this doesn't work your browser doesn't support line breaks in tooltips. See this post.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.