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,
}
My backbone project has no idea how to handle _id.$oid.
I found this article and code:
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
I'm new to Ruby and Rails and have no idea how to proceed, so any help is greatly appreciated
Moped::BSON::ObjectId
monkey patch can be placed anywhere (an initializer does sound like a good spot, though you should make a new initializer for just this code) and it should work. After you added that and restarted the rails server, did it work? What happened instead of what you expected?alias
, then use it properly:alias to_json to_s
. If you want to use a method where you actually have to pass symbols, use alias_method:alias_method :to_json, :to_s
.