Is there any way to use the to_json includes when using "render :json"? I''ve got this: format.json { render :json => { :order => @orders } } But I''d also like to include the address (order.address) association. The "to_json" method has all these options, but I don''t see how I can use that with render :json. If I tack on the to_json to @orders the render :json escapes the whole thing. I''m using Ext JS on the client side. Thanks! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Dec 20, 2:55 pm, "pat.osterday" <pat.oster...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Is there any way to use the to_json includes when using > "render :json"? > > I''ve got this: > > format.json { render :json => { :order => @orders } } >render :json => @orders.to_json(...) Fred> But I''d also like to include the address (order.address) association. > The "to_json" method has all these options, but I don''t see how I can > use that with render :json. If I tack on the to_json to @orders the > render :json escapes the whole thing. I''m using Ext JS on the client > side. > > Thanks!--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
use eager loading on your @orders with :include option or use :select to add attributes from address if you only need limited data from address and performance is a concern. Regards, rp8 ======================http://lun.competo.com/ Frederick Cheung wrote:> On Dec 20, 2:55�pm, "pat.osterday" <pat.oster...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Is there any way to use the to_json includes when using >> "render :json"? >> >> I''ve got this: >> >> format.json { render :json => { :order => @orders } } >> > > render :json => @orders.to_json(...) > > Fred-- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
pbo808-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Dec-22 14:31 UTC
Re: render :json and associations
This works, but what if I want to add other json object that aren''t in orders? Ext JS needs a "count" param for it''s client side pagination. So I originally had this: format.json { render :json => { :order => @orders, :count => @orders.size } } I couldn''t figure out how to use to_json in this situation. On Dec 20, 10:19 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > render :json => @orders.to_json(...) > > Fred >--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
pbo808-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Dec-22 14:33 UTC
Re: render :json and associations
I do have includes on the finder, but the associates are not sent with the json output. For now I wound up just building my own hash and returning that. On Dec 20, 1:55 pm, rp8 -_- <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> use eager loading on your @orders with :include option or use :select to > add attributes from address if you only need limited data from address > and performance is a concern. > > Regards, > > rp8 > =======================http://lun.competo.com/ > > Frederick Cheung wrote: > > On Dec 20, 2:55 pm, "pat.osterday" <pat.oster...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Is there any way to use theto_jsonincludes when using > >> "render :json"? > > >> I''ve got this: > > >> format.json { render :json => { :order => @orders } } > > > render :json => @orders.to_json(...) > > > Fred > > -- > Posted viahttp://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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Dec 22, 2:31 pm, "pbo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <pbo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This works, but what if I want to add other json object that aren''t in > orders? Ext JS needs a "count" param for it''s client side > pagination. So I originally had this: > > format.json { render :json => { :order => @orders, :count => > @orders.size } }format.json { render :json => { :order => @orders, :count => @orders.size }.to_json } Fred> > I couldn''t figure out how to use to_json in this situation. > > On Dec 20, 10:19 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > > > render :json => @orders.to_json(...) > > > Fred--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---