Have to_json return a mongoid as a string

前端 未结 6 962
名媛妹妹
名媛妹妹 2020-12-09 19:00

In my Rails API, I\'d like a Mongo object to return as a JSON string with the Mongo UID as an \"id\" property rather than as an \"_id\" object.

I want my API to retu

6条回答
  •  隐瞒了意图╮
    2020-12-09 19:29

    class Profile
      include Mongoid::Document
      field :name, type: String
      def to_json
        as_json(except: :_id).merge(id: id.to_s).to_json
      end
    end
    

提交回复
热议问题