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: "jmash@aol.com", 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

Jaqx
  • 826
  • 3
  • 14
  • 39

1 Answers1

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 %>
Mischa
  • 42,876
  • 8
  • 99
  • 111
  • This works, thanks! quick search indicates map and collect work the same way – Jaqx May 20 '13 at 13:55
  • @ayounesi, yeah they are actually aliases of each other. – Mischa May 20 '13 at 13:56
  • 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
    tag within the quotes title: "#{board.voters_who_voted.collect(&:name).join("\n")}" what i had. \n still does nothing
    – Jaqx May 20 '13 at 14:26
  • still no dice but Ill tinker with it some more – Jaqx May 20 '13 at 14:46
  • @ayounesi What's the problem with this code then? Why doesn't it work? Any errors? Unexpected behavior? – Mischa May 20 '13 at 14:47
  • just not line breaking between names – Jaqx May 20 '13 at 14:49
  • 1
    @ayounesi If this doesn't work your browser doesn't support line breaks in tooltips. See [this post](http://stackoverflow.com/questions/358874/how-can-i-use-a-carriage-return-in-a-html-tooltip). – Mischa May 20 '13 at 14:53
  • My problem may have been unique to my tooltip tipsy http://stackoverflow.com/questions/4491446/jquery-tooltip-that-supports-new-line – Jaqx May 20 '13 at 15:16