If I type in my console
u = User.first
u.friends(&map:username)
I get ["Peter", "Mary", "Jane"]
but I also want to show the birthday, so how do I do that? I tried
u.friends(&map:username, &map:birthday)
but this doesn't work.
You can use the alternate block syntax:
u.friends.map{|f| [f.username, f.birthday]}
which would give an array of arrays.
u.friends.map{|f| "#{f.username} - #{f.birthday}"}
would give you an array of strings. You can do quite a bit from there.
Try
u.friends.map {|friend| [friend.username, friend.birthday]}
The &
syntax is simply a shorthand to the underlying Ruby method.
使用ActiveRecord> = 4,您只需使用:
u.friends.pluck(:username, :birthday)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.