我有一個模型,返回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這是不一樣的問題:) –