0
board.voters_who_voted

返回一个包含来自 User 表的行的数组。我要提取的属性是名称。

示例输出(这里只有一个用户):

[#<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>]

我想我需要遍历整个数组并打印名称。

这是我尝试过的:

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

然而这只是出于某种原因返回整个数组

board.voters_who_voted[0].name

上面正确地返回了第一个用户的名字,但我需要这个给他们所有人

4

1 回答 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 %>
于 2013-05-20T13:48:37.103 回答