简体   繁体   English

如何将数据库对象作为哈希返回

[英]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.

相关问题 Rails中的胖模型:如何返回哈希(错误)或对象(成功时) - Fat model in Rails: How do I return hash (errors) or object (when success) 如何从值数组中搜索Ruby哈希,然后根据发现结果返回哈希? - How do I search a Ruby hash from an array of values, and then return a hash based on the findings? 如何在 Ruby 中访问此哈希对象的属性? - How do I access this hash's object's attributes in Ruby? 如何访问ruby对象内部数组中的哈希? - How do I access a hash inside an array inside a ruby object? 如何以突变形式返回自定义对象? - How do I return custom object in a mutation? 如何格式化此哈希? - How do I format this hash? 如何迭代哈希数据对象并将其存储在Rails的SQLite数据库中? - how can I iterate hash data object and store in SQLite database in rails? 如何使用哈希填充选择标记并将值正确保存到数据库? - How do I populate a select tag with a hash and properly save the value to the database? Ruby on Rails 2.3.8:如何进行自定义SQL查询,作为对象/ ActiveRecord对象返回,而不是哈希? - Ruby on Rails 2.3.8: How do I do a custom SQL query, returned as an object / ActiveRecord object, rather than a hash? 如何在RoR中为不同的对象实例强制使用相同的哈希值? - How do I force the same hash value for different object instantiations in RoR?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM