[英]How do I return a database object as a hash
Why does my application return @users = User.where(acceptance: true)
as #<User:0x007f9b0d444328>
? 为什么我的应用程序将
@users = User.where(acceptance: true)
作为#<User:0x007f9b0d444328>
?
when the console returns the same query as: 当控制台返回相同的查询时:
[#<User id: 1, acceptance: "t", created_at: "2012-09-27 13:01:50", updated_at: "2012-09-27 13:02:52">]
I want the users as a hash to pass to this sort of thing: 我希望用户作为哈希传递给这种事情:
respond_to do |format|
format.html
format.csv { render text: @users.to_csv }
end
@user.attributes
@users.map { |user| user.attributes }
This is a ruby hash of the fields in your database. 这是数据库中字段的ruby哈希值。 Then it will be up to you to encode it in JSON or CSV.
然后,您可以使用JSON或CSV对其进行编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.