wprater
2010-Aug-02 21:26 UTC
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''
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Conrad Taylor
2010-Aug-03 07:01 UTC
Re: Rails3-beta4: Calling to_json on an ActiveRecord::Relation causes an error
On Mon, Aug 2, 2010 at 2:26 PM, wprater <mc.willprater-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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> > 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'' > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Huet
2010-Aug-03 10:20 UTC
Re: Rails3-beta4: Calling to_json on an ActiveRecord::Relation causes an error
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
On Aug 3, 8:01 am, Conrad Taylor
<conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> On Mon, Aug 2, 2010 at 2:26 PM, wprater
<mc.willpra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > 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
>
>
>
> > 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''
>
> > --
> > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> > To unsubscribe from this group, send email to
> >
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/rubyonrails-talk?hl=en.
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
wprater
2010-Aug-04 23:08 UTC
Re: Rails3-beta4: Calling to_json on an ActiveRecord::Relation causes an error
Thanks, that worked for me. I''m not able to upgrade to RC1 quite yet. Cheers, -Will On Aug 3, 3:20 am, Huet <h...-j1senl6PQQNhbmWW9KSYcQ@public.gmane.org> wrote:> 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 > > On Aug 3, 8:01 am, Conrad Taylor <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > On Mon, Aug 2, 2010 at 2:26 PM, wprater <mc.willpra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Here is my model and an attempt to convert to json. Whether there is > > > data returned or not, acircularreference 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 > > > > 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'' > > > > -- > > > 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-/JYPxA39Uh4Ykp1iOSErHA@public.gmane.orgm. > > > To unsubscribe from this group, send email to > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com> > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.