简体   繁体   中英

How do I return a database object as a hash

Why does my application return @users = User.where(acceptance: true) as #<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. Then it will be up to you to encode it in JSON or CSV.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM