我有一个模型,返回JSON:如何使用mongoid
class Client
include Mongoid::Document
field :name, type: String
field :age, type: Integer
index({ name: 1 }, { unique: true })
def self.list
self.all.as_json
end
end
当我打电话Client.list我得到如下:
[{"client"=>{"_id"=>{"$oid"=>"58e91ccb9509d36cbaa8c79b"}, "name"=>"mark", "age"=>30}}]
我什么后:
[{"_id"=>{"$oid"=>"58e91ccb9509d36cbaa8c79b"}, "name"=>"mark", "age"=>30}]
我有同样的问题,所以我写了一个关于我如何解决它的博客。你可能会发现它很有帮助。 https://medium.com/aviabird/mongoid-how-to-serialize-to-json-with-a-string-id-attribute-2d5c1bc284a7 – VoidZero
@VoidZero这是不一样的问题:) –