Forum: Ruby on Rails render :json and associations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3f3771daa40effa1355df3fae83872c6?d=identicon&s=25 pat.osterday (Guest)
on 2008-12-20 16:19
(Received via mailing list)
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!
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-20 16:20
(Received via mailing list)
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
90e7e04c730e1b747288051af34c3bb0?d=identicon&s=25 rp8 -_- (rp8)
on 2008-12-20 19:55
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
C0902508ad1e18d9d13134ae9409ce21?d=identicon&s=25 pbo808@gmail.com (Guest)
on 2008-12-22 16:17
(Received via mailing list)
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>
C0902508ad1e18d9d13134ae9409ce21?d=identicon&s=25 pbo808@gmail.com (Guest)
on 2008-12-22 16:17
(Received via mailing list)
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.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-22 16:18
(Received via mailing list)
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
This topic is locked and can not be replied to.