6

假设你想

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",
    }
  }
  ...
]
4

1 回答 1

9

as_json

records.as_json(:root => true)

可序列化的哈希

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

不过,这不适用于嵌套对象

于 2013-06-13T15:22:34.530 回答