Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

There was a problem

You must be a member of this account.

This project is archived and is in readonly mode.

to_json problems with Array of 'models'

#2083

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'

Reported by NeoRiddle · February 26th, 2009 @ 09:53 AM

State: invalid
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. Coderifous
    Coderifous

    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.

    March 27th, 2009 @ 08:35 PM

  2. Ken Collins
    Ken Collins

    Me too, this ticket should be closed.

    July 19th, 2009 @ 02:09 AM

  3. Nate
    Nate

    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.

    July 29th, 2009 @ 05:17 PM

  4. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  5. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to invalid

    Closing ticket. Looks like a side effect.

    June 15th, 2010 @ 11:59 AM