This project is archived and is in readonly mode.
to_json problems with Array of 'models'
Reported by NeoRiddle | February 26th, 2009 @ 09:53 AM | in 3.x
Hi!
I have this code in controller:
class ChapSixController < ApplicationController
def map
@towers = Tower.all.to_json
end
end
I think there is a problem with transformming arrays with some model object (ActiveRecord::Base objects) by calling 'to_json' method. But testing this with another object like strings, numbers, etc. it does not happen.
Here you have the exception trace:
wrong number of arguments (1 for 0)
/usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/json/encoding.rb:21:in `to_json'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/json/encoding.rb:21:in `send'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/json/encoding.rb:21:in `encode'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/json/encoding.rb:31:in `raise_on_circular_reference'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/json/encoding.rb:20:in `encode'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/json/encoders/enumerable.rb:10:in `to_json'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/json/encoders/enumerable.rb:10:in `map'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/json/encoders/enumerable.rb:10:in `to_json'
app/controllers/chap_six_controller.rb:3:in `map'
Comments and changes to this ticket
-
Coderifous March 27th, 2009 @ 08:35 PM
Check your model if you overrode the to_json method from ActiveRecord, it needs to accept optional ... options.
It should match to_json(options = {})
I had the problem and that was the issue.
-
Nate July 29th, 2009 @ 05:17 PM
You could also be requiring something from the json gem somewhere else which is overriding ActiveSupport's overriding of the json gem's to_json. Code explains it better:
http://github.com/rails/rails/blob/a147becfb86b689ab25e92edcfbb4bcc...
Then maybe in config/initializers/foo.rb or in a lib or something you're doing:
require 'json/objects'
I just had this issue and it was a pain to track down. Hope that helps.
-
Rohit Arondekar June 15th, 2010 @ 11:59 AM
- State changed from new to invalid
Closing ticket. Looks like a side effect.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>