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.