红宝石模型输出ID作为对象OID [英] Ruby model output id as object oid

查看:146
本文介绍了红宝石模型输出ID作为对象OID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的红宝石的模型,像这样:

My ruby model, like so:

class User
  include Mongoid::Document
  field :first_name, type: String
  field :birthdate, type: Date

  validates :first_name, :birthdate, :presence => true

end

输出的对象,像这样:

outputs an object like so:

{
_id: {
$oid: "522884c6c4b4ae5c76000001"
},
birthdate: null,
first_name: null,
}

我的骨干工程已经不知道该如何处理_id。$ OID。

My backbone project has no idea how to handle _id.$oid.

我发现这篇文章和code:

I found this article and code:

<一个href=\"https://github.com/rails-api/active_model_serializers/pull/355/files\">https://github.com/rails-api/active_model_serializers/pull/355/files

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

我不知道在哪里把这个,以及如何调用它的模式输出,所以我尝试内部:

I have no idea where to put this, and how to invoke it on the model output, so I tried inside:

/config/initializers/secret_token.rb

/config/initializers/secret_token.rb

我是新来的Ruby和Rails,而且不知道如何着手,因此任何帮助是极大的AP preciated

I'm new to Ruby and Rails and have no idea how to proceed, so any help is greatly appreciated

推荐答案

你应该做的就是这个地方在初始化文件夹中,创建一个这样的文件:

What you should do is place this in the initializer folder, create a file like this:

/config/initializers/mongoid.rb

/config/initializers/mongoid.rb

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

这篇关于红宝石模型输出ID作为对象OID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆