# Circular reference error on #to\_json

**URL:** <https://www.ruby-forum.com/t/circular-reference-error-on-to-json/106264>\
**Category:** Rails\
**Created:** [June 7, 2007, 10:34pm UTC](https://www.ruby-forum.com/t/circular-reference-error-on-to-json/106264 "2007-06-07T22:34:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cjharrelson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c89c15/32.png) [@cjharrelson](https://www.ruby-forum.com/u/cjharrelson)\
**Post date:** [June 7, 2007, 10:34pm UTC](https://www.ruby-forum.com/t/circular-reference-error-on-to-json/106264/1 "2007-06-07T22:34:37Z")

</div>

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:in`encode’  
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:in`map’  
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:in`encode’  
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:in`encode’  
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:in`map’  
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:in`map’  
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:in`call’  
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:in`raise\_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:in`to\_json’  
#{RAILS\_ROOT}/app/controllers/movies\_controller.rb:41:in `grid\_data’

Any ideas? I am stumped.

---

<div class="post-metadata">

**Author:** ![cjharrelson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c89c15/32.png) [@cjharrelson](https://www.ruby-forum.com/u/cjharrelson)\
**Post date:** [June 9, 2007, 1:07am UTC](https://www.ruby-forum.com/t/circular-reference-error-on-to-json/106264/2 "2007-06-09T01:07:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cjharrelson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c89c15/32.png) [@cjharrelson](https://www.ruby-forum.com/u/cjharrelson)\
**Post date:** [June 28, 2007, 4:17pm UTC](https://www.ruby-forum.com/t/circular-reference-error-on-to-json/106264/3 "2007-06-28T16:17:46Z")

</div>

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](http://www.eribium.org/blog/?p=106)
