如何将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() }
}