9

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

2
  • 1
    That 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?
    – Alex Wayne
    Sep 5, 2013 at 22:13
  • Please, if you use the syntax construct 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.
    – apeiros
    Feb 27, 2018 at 18:56

5 Answers 5

33

Iterating on Kirk's answer:

In Mongoid 4 the Moped’s BSON implementation has been removed in favor of the MongoDB bson gem, so the correct version for Mongoid 4 users is:

module BSON
  class ObjectId
    def to_json(*args)
      to_s.to_json
    end

    def as_json(*args)
      to_s.as_json
    end
  end
end
2
  • This worked as well, just don't forget to restart your server and reset any caching if your API's are cached (had to figure that out the hard way)...
    – netwire
    May 11, 2015 at 16:35
  • 2
    This worked for me: If you add the alias :to_json :to_s solution as specified in previous answers, you'll get the following error: ArgumentError: wrong number of arguments (1 for 0)
    – ddreliv
    Oct 15, 2015 at 20:48
7

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

/config/initializers/mongoid.rb

module Moped
  module BSON
    class ObjectId
      alias :to_json :to_s
      alias :as_json :to_s
    end
  end
end
2
  • Do I need to define a way to include that or does it automatically include initializers? I created the file and its still outputting as _id.$oid
    – azz0r
    Sep 6, 2013 at 11:51
  • 1
    hi @azz0r i forget to add the alias on the as_json method too. Try that code. About the initializer folder, rails runs through all files inside config/initializers/. and run them all. Sep 6, 2013 at 13:58
4

For guys using Mongoid 4+ use this,

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

Reference

3

Aurthur's answer worked for everything except rabl. If you are using rabl the attributes :id will throw an exception. The following code will be compatible with rabl.

module Moped
  module BSON
    class ObjectId
      def to_json(*args)
        to_s.to_json
      end

      def as_json(*args)
        to_s.as_json
      end
    end
  end
end

For more information, see the github issue https://github.com/nesquena/rabl/issues/337

0

Here is a better answer

require "bson"

class Jbuilder < JbuilderProxy

  def _extract_method_values(object, *attributes)
    attributes.each do |key|
      value = object.public_send(key)

      if value.is_a? ::BSON::ObjectId
        value = value.to_s
      end

      _set_value key, value
    end
  end
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.