Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


Mongoid::Criteria#to_json breaks in Rails 3.0.2+ #458

cgriego opened this Issue · 23 comments

This Rails core commit is what changed.

The exception I get when I do MyModel.all.to_json is:

NoMethodError: undefined method `encode_json' for #<MyModel:0x104f834a0>
    from gems/mongoid-2.0.0.beta.20/lib/mongoid/attributes.rb:23:in `method_missing'
    from gems/activesupport-3.0.3/lib/active_support/json/encoding.rb:214:in `encode_json'
    from gems/activesupport-3.0.3/lib/active_support/json/encoding.rb:214:in `map'
    from gems/activesupport-3.0.3/lib/active_support/json/encoding.rb:214:in `encode_json'
    from gems/activesupport-3.0.3/lib/active_support/json/encoding.rb:47:in `encode'
    from gems/activesupport-3.0.3/lib/active_support/json/encoding.rb:77:in `check_for_circular_references'
    from gems/activesupport-3.0.3/lib/active_support/json/encoding.rb:45:in `encode'
    from gems/activesupport-3.0.3/lib/active_support/json/encoding.rb:30:in `encode'
    from gems/activesupport-3.0.3/lib/active_support/core_ext/object/to_json.rb:15:in `to_json'

A workaround is to call to_a.to_json, and this is essentially how ActiveRecord::Relation works, #as_json calls to_a.as_json and to_xml is delegated to to_xml.


I'm getting the same error.


Getting this too with Rails 3.0.3. Here's the rails ticket for that commit that cgriego mentioned:


Same as the others.




I believe this effects everyone with Rails 3.0.3+. Is anyone actively working on a fix for this?




I found that this will let me avoid the issue for the time being:

gem 'activesupport', "< 3.0.2"
gem 'mongo'
gem 'mongoid', '2.0.0.beta.20'

is this bug fixed also have the same problem


Nothing I've seen yet, @devcorkvince. I tried to take a look at it myself but it appears to touch quite a few things, so I was hoping one of the committers would have a more intimate knowledge of how this could be changed. The workaround for now is to stick with Rails 3.0.1.


Here is an option for freedom patching (aka monkey patching) around the issue for now.

class Mongoid::Criteria
  delegate :as_json, :to => :to_a

I put this in config/initializers/mongoid_hacks.rb within my rails app for now.

I'm willing to look into supplying a pull-request for this, but I'm not really sure where it belongs. It doesn't look like Mongoid really deals with serialization anywhere else. It'd be easy to throw it into criteria.rb, but it doesn't look like it belongs there.


@rmm5t, thanks for the fix!

I wouldn't put in a pull request if this is a bug/unexpected behavior, since then you'd have version-check it, right?

Unless you know that this is an expected/correct behavior.




hmm I think this would help but not.
Now I have same error with rails '3-0-stable' and 'master' branches
Please create literate ticket on


Override Criteria#as_json to delegate to the entries. Closed by 65932ce.


Is this in a 2.0.0 rc?


@gboruk - It hasn't been released to a gem version yet, but will be in the next one. In the meantime, I'm using the fix from @rmm5t above and it's working without issue.


@doxavore thanks!

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.