假设你想
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)
serializable_hash
records.to_a.map() {|x|
{ x.class.model_name.element => x.serializable_hash() }
}
虽然这不适用于嵌套对象
关于activerecord - 如何将 activerecord 结果转换为包含根的哈希数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17090891/