Rails3-beta4: Calling to_json on an ActiveRecord::Relation causes an error

Here is my model and an attempt to convert to json. Whether there is data returned or not, a circular reference error is thrown. How can I avoid this?

ruby-1.8.7-p249 > PayPeriod => PayPeriod(id: integer, start_date: date, end_date: date, pay_date: date) ruby-1.8.7-p249 > PayPeriod.where('end_date < ?', Time.now) => ruby-1.8.7-p249 > PayPeriod.where('end_date < ?', Time.now).to_json ActiveSupport::JSON::Encoding::CircularReferenceError: object references itself   from /Users/wprater/.rvm/gems/ruby-1.8.7-p249/gems/ activesupport-3.0.0.beta4/lib/active_support/json/encoding.rb:59:in `check_for_circular_references'

Here is my model and an attempt to convert to json. Whether there is

data returned or not, a circular reference error is thrown. How can I

avoid this?

wprater, I would recommend trying Rails 3 RC by doing the following:

gem install rails --pre

Also, if you don’t have a requirement to use Ruby 1.8.7, I would recommend

using Ruby 1.9.2 RC2 by doing the following:

rvm install 1.9.2

Ryan Bates recently added the following screencast which covers the installation

of both Rails 3 and Ruby 1.9.2.

http://media.railscasts.com/videos/225_upgrading_to_rails_3_part_1.mov

Good luck,

-Conrad

Hi All,

I have had this problem with Rails 3 and 1.9.1. The fix for this error is to add to_a to the json call.

render :json => {:top => @top.to_a.as_json(:only => [:id, :title ] ) }

Hope this helps.

Huet

Thanks, that worked for me. I'm not able to upgrade to RC1 quite yet.

Cheers,

-Will