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.

This project is archived and is in readonly mode.

[PATCH] ActiveRecord live scope #to_json fix. Prevents the CircularReferenceError error

#4943

Calling #to_json on a live ActiveRecord scope will always raise the following error:

ActiveSupport::JSON::Encoding::CircularReferenceError: object references itself

This is because entity that gets passed to the JSON encoder is the ActiveRecord::Relation instance instead of the loaded array of records.

Started noticing this error when working on a Rails 3.beta4 app and using the respond_with(@records) call on my controller without firing off the scope:

respond_to :html, :json

def index
  @users = User.most_recent
  respond_with(@users)
end

Patch overrides the #to_json method on ActiveRecord::Relation so that it makes sure the scope is loaded and the method is called on the @records collection.

Test is included that fails without the patch.

Reported by Jose Fernandez · June 23rd, 2010 @ 05:45 AM

State: duplicate
Milestone: none
Assigned to: nobody
Importance: Low