如何将activerecord结果转换为包含根的哈希数组

如何将activerecord结果转换为包含根的哈希数组,activerecord,hash,Activerecord,Hash,假设你想 records = Model.all records.to_a.map{|m| m.serializable_hash(:root => true)} 就像to_json(:root=>true)一样 [ { "model": { "attribute_1": "value_1", "attribute_2": "value_2", } } ... ] as_json 可序列化的_散列 但这不适用于嵌套对象 records

假设你想

records = Model.all
records.to_a.map{|m| m.serializable_hash(:root => true)}
就像
to_json(:root=>true)
一样

[
  {
    "model": {
      "attribute_1": "value_1",
      "attribute_2": "value_2",
    }
  }
  ...
]
as_json 可序列化的_散列 但这不适用于嵌套对象

records.as_json(:root => true)
records.to_a.map() {|x| 
  { x.class.model_name.element => x.serializable_hash() } 
}