【发布时间】:2014-05-06 21:58:56
【问题描述】:
在我的 Rails API 中,我希望 Mongo 对象以 JSON 字符串的形式返回,Mongo UID 作为“id”属性而不是“_id”对象。
我希望我的 API 返回以下 JSON:
{
"id": "536268a06d2d7019ba000000",
"created_at": null,
}
代替:
{
"_id": {
"$oid": "536268a06d2d7019ba000000"
},
"created_at": null,
}
我的型号代码是:
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
【问题讨论】:
标签: ruby-on-rails mongoid