MongoDB Make mongoDB and spine.js sit well together

kevinhua · August 31, 2012 · 2183 hits

因为 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 实现。

No Reply at the moment.
You need to Sign in before reply, if you don't have an account, please Sign up first.