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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On Jun 7, 3:23 pm, cjharrelson <cjharrel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---