簡體   English   中英

Ember-data和MongoDB,如何處理_id

[英]Ember-data and MongoDB, how to handle _id

我將ember-data與rails和MongoDB結合使用,並且ID在MongoDB中的存儲方式存在問題-在_id字段中。

Ember-data將使用id作為ID的默認字段,因此我嘗試像這樣覆蓋它:

App.User = DS.Model.extend
    primaryKey: "_id"
    name: DS.attr "string"
    image: DS.attr "string"

這似乎在大多數時間都有效,但是在某些情況下,余燼會說:

未捕獲的錯誤:斷言失敗:您的服務器返回了帶有鍵_id的哈希,但是您沒有映射

我懷疑這可能是ember-data中的錯誤,因為它仍處於開發階段,但是我試圖找到一種方法來將_id映射到Rails服務器端的id上? 我正在使用mongoid進行mongo映射。

如果您使用的是Mongoid,那么這里有一個解決方案,因此您不必添加方法def id; object._id.to_s; end def id; object._id.to_s; end def id; object._id.to_s; end每個序列化器

添加以下Rails初始化程序

Mongoid 3.x

module Moped
  module BSON
    class ObjectId
      alias :to_json :to_s
      alias :as_json :to_s
    end
  end
end

Mongoid 4

module BSON
  class ObjectId
    alias :to_json :to_s
    alias :as_json :to_s
  end
end

用於Building活動模型串行器

class BuildingSerializer < ActiveModel::Serializer
  attributes :id, :name
end

結果JSON

{
  "buildings": [
    {"id":"5338f70741727450f8000000","name":"City Hall"},    
    {"id":"5338f70741727450f8010000","name":"Firestation"}
  ]
}

這是一個猴子通過補丁建議brentkirby和更新的Mongoid 4 arthurnn

另一種方法是使用ActiveModel :: Serializer (如果可能的話)。 (我認為它應該接近rabl(?))

從ember-data gihtub: https : //github.com/emberjs/data
對遵循active_model_serializers gem約定的Rails應用程序的開箱即用支持

當我們從ember-data開始時,我們正在制作as_json() ,但是使用gem絕對更好:)

啊,您可以將JSON改為使用id方法而不是_id屬性,而不是在JSON中包含_id。 方法:

您可以使用rabl ,JSON可能像這樣:

object @user 
attributes :id, :email
node(:full_name) {|user| "#{user.first_name} #{user.last_name}"}

您也可以制作as_json方法

class User
  def as_json(args={})
    super args.merge(:only => [:email], :methods => [:id, :full_name])
  end
end

我在將ember.js與ember-resource和couchdb一起使用時遇到了類似的問題,該文件還將其ID存儲為_id

作為此問題的解決方案,我為我的所有模型類定義了一個超類,其中包含一個將_id復制到id的計算屬性,如下所示:

// get over the fact that couchdb uses _id, ember-resource uses id
id: function(key, value) {
    // map _id (couchdb) to id (ember)
    if (arguments.length === 1) {
        return this.get('_id');
    }
    else {
        this.set('_id', value);
        return value;
    }
}.property('_id').cacheable()

也許這也可以解決您的問題?

最好的方法是使用ActiveModel::Serializers 由於我們使用的是Mongoid ,因此您需要添加一個include語句(請參見benedikt的要點 ):

# config/initializers/active_model_serializers.rb
Mongoid::Document.send(:include, ActiveModel::SerializerSupport)
Mongoid::Criteria.delegate(:active_model_serializer, :to => :to_a)

然后包括您的序列化器。 像這樣:

# app/serializers/user_serializer.rb
class UserSerializer < ActiveModel::Serializer
  attributes :id, :name, :email
  def id
    object._id
  end 
end

這解決了_id問題

joscas回答的第二部分使用Rails4 / Ruby2為我解決了id問題,除了我必須.to_s _id。

class UserSerializer < ActiveModel::Serializer
  attributes :id, :name, :email
  def id
    object._id.to_s
  end
end

如果您使用Mongoid3,以下是猴子補丁可能適合您的情況。

https://gist.github.com/4700909

我不知道是什么時候添加的,但是您可以告訴Ember-Data primaryKey是_id:

DS.RESTAdapter.extend({
  serializer: DS.RESTSerializer.extend({
    primaryKey: '_id'
  })
});

盡管這個問題已經很老了,但我仍然認為我的回答可以幫助其他人:

如果您使用的是ActiveModelSerializer,則只需執行以下操作:

class UserSerializer < ActiveModel::Serializer
     attributes :id , :name
end

一切正常。 我在前端btw上使用emberjs。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM