This project is archived and is in readonly mode.

#4943 ✓duplicate
Jose Fernandez

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

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

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

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.

Comments and changes to this ticket

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=""></a>