在我的Rails API中,我希望Mongo对象作为JSON字符串返回,其中Mongo UID是"id"属性而不是"_id"对象.
我希望我的API返回以下JSON:
{
"id": "536268a06d2d7019ba000000",
"created_at": null,
}
Run Code Online (Sandbox Code Playgroud)
代替:
{
"_id": {
"$oid": "536268a06d2d7019ba000000"
},
"created_at": null,
}
Run Code Online (Sandbox Code Playgroud)
我的型号代码是:
class Profile
include Mongoid::Document
field :name, type: String
def to_json(options={})
#what to do here?
# options[:except] ||= :_id #%w(_id)
super(options)
end
end
Run Code Online (Sandbox Code Playgroud)