Circular reference error on #to_json

I am trying to create a json response that can be used to create a
paging grid. Here is my controller code:

def grid_data
start = (params[:start] || 1).to_i
size = (params[:limit] || 20).to_i
sort_col = (params[:sort] || ‘id’)
sort_dir = (params[:dir] || ‘ASC’)

  page = ((start/size).to_i)+1

  @movie_pages = Paginator.new(self, Movie.count, size, page)

  @movies = Movie.find(:all,
                       :select => "id, title, plot, date, genre,

mpaa, directed_by",
:limit=>@movie_pages.items_per_page,
:offset=>@movie_pages.current.offset,
:order=>sort_col+’ '+sort_dir)

  return_data = Hash.new()
  return_data[:Total] = @movie_pages.item_count
  return_data[:Movies] = @movies.collect{ |u| {:id=>u.id,
                                      :title=>u.title,
                                      :plot=>u.plot,
                                      :date=>u.date,
                                      :genre=>u.genre,
                                      :mpaa=>u.mpaa,
                                      :directed_by=>u.directed_by} }
  render :json => return_data.to_json

end

and I am getting an object references itself error with this stack
trace:

C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
json.rb:39:in raise_on_circular_reference' C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ json.rb:26:inencode’
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
core_ext/object/misc.rb:32:in to_json' C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ json/encoders/core.rb:49 C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ json/encoders/core.rb:49:inmap’
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
json/encoders/core.rb:49
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
json.rb:27:in call' C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ json.rb:27:inencode’
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
json.rb:42:in raise_on_circular_reference' C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ json.rb:26:inencode’
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
core_ext/object/misc.rb:32:in to_json' C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ json/encoders/core.rb:57 C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ base.rb:1940:inmap’
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
json/encoders/core.rb:54:in each' C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ json/encoders/core.rb:54:inmap’
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
json/encoders/core.rb:54
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
core_ext/object/misc.rb:23:in returning' C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ json/encoders/core.rb:53 C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ json.rb:27:incall’
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
json.rb:27:in encode' C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ json.rb:42:inraise_on_circular_reference’
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
json.rb:26:in encode' C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ core_ext/object/misc.rb:32:into_json’
#{RAILS_ROOT}/app/controllers/movies_controller.rb:41:in `grid_data’

Any ideas? I am stumped.

Figured out that this has something to do with the attribute that is a
date. I I take it out, it works just fine. Hopefully I can figure
out the problem with the date.

cjharrelson wrote:

Figured out that this has something to do with the attribute that is a
date. I I take it out, it works just fine. Hopefully I can figure
out the problem with the date.

You may try patching the date processing with in your environment

ActiveSupport::JSON::Encoders.define_encoder Date do |date|
date.to_s
end

Source: http://www.eribium.org/blog/?p=106