Building JSON Doc with certain rows

Hi all,

I’m building a site that delivers some data to a mobile device using
JSON.

The data is all about music events, and each event has_many bands, and
belongs_to a venue, and these associated models i’m also including in my
JSON doc.

The problem is, I don’t need to include all the columns for every
associated model, ie bands, I just want their name.

I’ve tried putting the :select line in the event model eg,

has_and_belongs_to_many :bands , :select => [“band_name”, “id”]

but then I lose attributes I want in other parts of the website. When
building a new event for example, I also want the band_descriptions -
exactly the column I don’t want in the JSON doc because it can be a lot
of text.

How would I modify the call to render the json doc to only include
certain attributes from associated models?? Here’s the call at the
moment :

format.json { render :json => @ed_events, :include => [:venue, :tickets,
:bands, :image, :headline_band]}

For bands, I only want the band_name here, but want other columns in
other parts of the app, so don’t want to do the general :select override
as shown above.

All help appreciated.

M

Michael B. wrote in post #1007737:

How would I modify the call to render the json doc to only include
certain attributes from associated models?? Here’s the call at the
moment :

format.json { render :json => @ed_events, :include => [:venue, :tickets,
:bands, :image, :headline_band]}

For bands, I only want the band_name here, but want other columns in
other parts of the app, so don’t want to do the general :select override
as shown above.

Have you tried the :only or :exclude options on to_json?

Michael B. wrote in post #1007855:

Have you tried the :only or :exclude options on to_json?

Sorry but could you tell me how I could actually do this ie something
like?

format.json { render :json => @ed_events, :include => [:venue, options
=> [:only => “venue_name”], :tickets]

or does the :only option only work on the :json command like

format.json { render :json => @ed_events, :include => [:venue,
:tickets], :only => [:venue => “venue_name”]]

I just have no idea how to implement the :only / :exclude options that
you mention!!

Should be just another option in the options hash like :include…

render :json => @ed_events, :only => [ :venue_name ], :include => [
:venue, :tickets ]

render :json => @ed_events, :only => [ :venue_name ], :include => [
:venue, :tickets ]

I see I see, thanks! I didn’t expect include / exclude to work on
associated models, but that’s really useful.

The slight complication is that I’d like to include the description
attribute for events, but not include it for bands, do you know if it’s
possible to specify which model you are including / excluding the
attribute from?? The workaround of course would be to change my column
names to be unique

Cheers

Have you tried the :only or :exclude options on to_json?

Sorry but could you tell me how I could actually do this ie something
like?

format.json { render :json => @ed_events, :include => [:venue, options
=> [:only => “venue_name”], :tickets]

or does the :only option only work on the :json command like

format.json { render :json => @ed_events, :include => [:venue,
:tickets], :only => [:venue => “venue_name”]]

I just have no idea how to implement the :only / :exclude options that
you mention!!