MongoDB Make mongoDB and spine.js sit well together

kevinhua · 2012年08月31日 · 2183 次阅读

因为 mongoDB 的默认 identity 是字符串,通过 huacnlee 的 mongoid_auto_increment_id 可以覆盖为 Integer,但使用 to_json 方法后的 Hash 中有“_id",而没有“id",通过在 model 中加入下面的方法覆盖,可以很好地解决。

def as_json(options={})
  result = super(:only => [:_id, :name, :slug, :body])
  result["id"] = result["_id"]
  result.delete("_id")
  result
end

如果怕麻烦,还可以直接写进 MongoID Document 模块,使用 included 实现。

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请 注册新账号