Ember-data and MongoDB, how to handle _id

后端 未结 9 810
别跟我提以往
别跟我提以往 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:35

    Ahh, instead of including _id in your JSON, you could craft the JSON to instead use the id method rather than the _id attribute. Ways:

    You could use rabl, and the JSON could be like:

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

    You could also craft the as_json method

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

提交回复
热议问题