Ember-data and MongoDB, how to handle _id

后端 未结 9 814
别跟我提以往
别跟我提以往 2021-02-08 17:20

I\'m using ember-data with rails and MongoDB and am having problem with the way IDs are stored in MongoDB - in a _id field.

Ember-data will use id as the default field f

9条回答
  •  一生所求
    2021-02-08 17:57

    The best way is to use ActiveModel::Serializers. Since we are using Mongoid, you will need to add an include statement like that (see this gist from benedikt):

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

    And then include your serializer. Something like that:

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

    This fixes the _id problem

提交回复
热议问题