I am fetching the json data like.
我正在获取json数据。
users_json = User.all.to_json
And storing as arry hash like.
像arry hash一样储存。
arry = JSON.parse(users_json)
And than fetching name & email, to use for file write, right now I am doing this simple in multiple lines,
而不是取名字和电子邮件,用于文件写,现在我在多行中做这个简单的,
email = arry.map { |x| x["email"] }
name = arry.map { |x| x["name"] }
And pretty sure there will one line exist for do so. Please help me to improve my logic.
很肯定会有一行这样做。请帮助我改进我的逻辑。
2 个解决方案
#1
2
For rails 3
rails 3
User.select([:name, :email]).to_json
And for rails 4 no need of the array
对于rails 4,不需要数组
User.select(:name, :email).to_json
Refer to the APIdock for more on what select does.
有关select功能的更多信息,请参阅APIdock。
#2
1
Another possible way can be
另一种可能的方法是
User.all.map{|x| [x.name, x.email]}.to_json
But really there is no point to convert into json.
但实际上,把它转换成json是毫无意义的。
#1
2
For rails 3
rails 3
User.select([:name, :email]).to_json
And for rails 4 no need of the array
对于rails 4,不需要数组
User.select(:name, :email).to_json
Refer to the APIdock for more on what select does.
有关select功能的更多信息,请参阅APIdock。
#2
1
Another possible way can be
另一种可能的方法是
User.all.map{|x| [x.name, x.email]}.to_json
But really there is no point to convert into json.
但实际上,把它转换成json是毫无意义的。