Jamie
Jamie

Reputation: 786

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

Upvotes: 0

Views: 114

Answers (1)

Marlin Pierce
Marlin Pierce

Reputation: 10099

@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.

Upvotes: 2

Related Questions