render :json and associations

410 views
Skip to first unread message

pat.osterday

unread,
Dec 20, 2008, 9:55:45 AM12/20/08
to Ruby on Rails: Talk
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!

Frederick Cheung

unread,
Dec 20, 2008, 10:19:41 AM12/20/08
to Ruby on Rails: Talk


On Dec 20, 2:55 pm, "pat.osterday" <pat.oster...@gmail.com> 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

rp8 -_-

unread,
Dec 20, 2008, 1:55:21 PM12/20/08
to rubyonra...@googlegroups.com
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...@gmail.com> 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/.

pbo...@gmail.com

unread,
Dec 22, 2008, 9:31:44 AM12/22/08
to Ruby on Rails: Talk
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...@gmail.com>
wrote:

pbo...@gmail.com

unread,
Dec 22, 2008, 9:33:20 AM12/22/08
to Ruby on Rails: Talk
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...@andreas-s.net> 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...@gmail.com> wrote:
> >> Is there any way to use theto_jsonincludes when using

Frederick Cheung

unread,
Dec 22, 2008, 10:17:26 AM12/22/08
to Ruby on Rails: Talk


On Dec 22, 2:31 pm, "pbo...@gmail.com" <pbo...@gmail.com> 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
Reply all
Reply to author
Forward
0 new messages