Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
cgriego opened this Issue · 23 comments
@cgriego

This Rails core commit is what changed.
rails/rails@2524cf4#diff-3

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.

@christian

I'm getting the same error.

@lapluviosilla

Getting this too with Rails 3.0.3. Here's the rails ticket for that commit that cgriego mentioned: https://rails.lighthouseapp.com/projects/8994/tickets/5374-as_json-should-return-a-hash-for-activemodelactiverecord-objects

@zenom

Same as the others.

@rand99

same

@doxavore

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

@arbales

+1

@arbales

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'
@devcorkvince

is this bug fixed also have the same problem

@doxavore

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.

@rmm5t

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

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

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.

@arbales

@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.

@gboruk

+1

@marser

hmm I think this https://rails.lighthouseapp.com/projects/8994/tickets/6077-activesupportjsonencode-fails-for-struct-types#ticket-6077-20 would help but not.
Now I have same error with rails '3-0-stable' and 'master' branches
Please create literate ticket on rails.lighthouseapp.com

@durran
Owner

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

@gboruk

Is this in a 2.0.0 rc?

@doxavore

@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.

@gboruk

@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.