Mongoid _id field returns object instead of String #337

Closed
sethvargo opened this Issue Oct 1, 2012 · 7 comments

Comments

Projects
None yet
6 participants
@sethvargo

When trying to access the :_id field like so:

object @object
attributes :_id, ...

the rendered JSON is:

"id": {
  "$oid": "504f8163a3bbccc81b000007"
},

but the rendered JSON when simply calling to_json is:

"_id": "504f8163a3bbccc81b000007"

I think it's because to_json implicitly calls to_s on the field, but not 100% sure on that one.

How can I actually return the object_id as a string? I really don't want to create an attribute to call to_s for each of my models.

@ccocchi

This comment has been minimized.

Show comment Hide comment
@ccocchi

ccocchi Oct 1, 2012

Contributor

That how BSON objectId are encoded as JSON (see here).

One way would be to replace attributes :id by node(:id) { |o| i.id.to_s } or you can override the way objectId are translated to JSON

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

ccocchi commented Oct 1, 2012

That how BSON objectId are encoded as JSON (see here).

One way would be to replace attributes :id by node(:id) { |o| i.id.to_s } or you can override the way objectId are translated to JSON

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

This comment has been minimized.

Show comment Hide comment
@sethvargo

sethvargo Oct 1, 2012

What do you think are the repercussions of overriding the to_json method?

What do you think are the repercussions of overriding the to_json method?

@nesquena

This comment has been minimized.

Show comment Hide comment
@nesquena

nesquena Oct 1, 2012

Owner

Well overriding it on particular classes can be reasonably safe especially when you just transform the data and then re-invoke to_json as is shown in the example. I have this type of override in some projects and in others I just do node(:id) { |o| i.id.to_s }. Haven't run into any issues yet.

Owner

nesquena commented Oct 1, 2012

Well overriding it on particular classes can be reasonably safe especially when you just transform the data and then re-invoke to_json as is shown in the example. I have this type of override in some projects and in others I just do node(:id) { |o| i.id.to_s }. Haven't run into any issues yet.

@GauravShetty1016

This comment has been minimized.

Show comment Hide comment
@GauravShetty1016

GauravShetty1016 Oct 11, 2012

I seem to be having this issue in 0.7.2 version of the gem. As of 0.7.1 when I use attributes :id the to_s function seems to be getting called on it.

I seem to be having this issue in 0.7.2 version of the gem. As of 0.7.1 when I use attributes :id the to_s function seems to be getting called on it.

@nesquena

This comment has been minimized.

Show comment Hide comment
@nesquena

nesquena Nov 15, 2012

Owner

I am going to close this because it really isn't a rabl issue at all. And there's defined solutions above. Thanks guys.

Owner

nesquena commented Nov 15, 2012

I am going to close this because it really isn't a rabl issue at all. And there's defined solutions above. Thanks guys.

@nesquena nesquena closed this Nov 15, 2012

@senfo

This comment has been minimized.

Show comment Hide comment
@senfo

senfo Aug 2, 2013

Am I missing something here? This seems to be a breaking change from earlier versions.

senfo commented Aug 2, 2013

Am I missing something here? This seems to be a breaking change from earlier versions.

@netwire88

This comment has been minimized.

Show comment Hide comment
@netwire88

netwire88 May 11, 2015

If you are using Mongoid 4, which changed how id are used using BSON, check out the answer here by updating mongoid.rb: http://stackoverflow.com/questions/18646223/ruby-model-output-id-as-object-oid/20813109

If you are using Mongoid 4, which changed how id are used using BSON, check out the answer here by updating mongoid.rb: http://stackoverflow.com/questions/18646223/ruby-model-output-id-as-object-oid/20813109

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment