quick question.
快速的问题。
If I type in my console
如果我输入控制台
u = User.first
u.friends(&map:username)
I geht ["Peter", "Mary", "Jane"]
我看到[彼得,玛丽,简]
But I also want to show the birthday, so how do I do that?
但是我也想展示生日,我该怎么做呢?
u.friends(&map:username, &map:birthday)
doesn't work thanks in advance
不用提前谢
2 个解决方案
#1
33
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.
会给你一个字符串数组。你可以从那里做很多。
#2
4
Try
试一试
u.friends.map {|friend| [friend.username, friend.birthday]}
The &
syntax is simply a shorthand to the underlying Ruby method.
&语法只是底层Ruby方法的简写。
#1
33
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.
会给你一个字符串数组。你可以从那里做很多。
#2
4
Try
试一试
u.friends.map {|friend| [friend.username, friend.birthday]}
The &
syntax is simply a shorthand to the underlying Ruby method.
&语法只是底层Ruby方法的简写。