我正在使用带有rails和MongoDB的ember-data,并且在_id字段中存在MongoDB中的ID存在问题.
Ember数据将使用ID作为ID的默认字段,因此我尝试重写它,如下所示:
App.User = DS.Model.extend primaryKey: "_id" name: DS.attr "string" image: DS.attr "string"
这似乎在大部分时间都可以工作,但在某些情况下,我从Ember中得到例外:
Uncaught Error: assertion Failed: Your server returned a hash with the
key _id but you have no mappings
我怀疑这可能是垃圾数据中的一个错误,因为它仍然在开发中,但是我试图找到一种方法来在服务器端映射_id到id?我正在使用mongoid来做mongo映射.
解决方法
如果你在这里使用Mongoid,那就是一个解决方案,使得你不必添加一个方法def id; object._id.to_s;结束到每个序列化器
添加以下Rails初始化程序
Mongoid 3.x
module Moped module BSON class ObjectId alias :to_json :to_s alias :as_json :to_s end end end
蒙古4
module BSON class ObjectId alias :to_json :to_s alias :as_json :to_s end end
活动模型序列化程序
class BuildingSerializer < ActiveModel::Serializer attributes :id,:name end
产生的JSON
{ "buildings": [ {"id":"5338f70741727450f8000000","name":"City Hall"},{"id":"5338f70741727450f8010000","name":"Firestation"} ] }
这是brentkirby建议的猴子补丁,并在arthurnn之前更新了Mongoid 4
原文地址:https://www.jb51.cc/ruby/265815.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。