Beyond the simple select

I have 3 tables:
pupils: id, name, born_date, sex
courses: id, course_name, description
pupilXcourse: pupil_id, course_id

Today I run a Pupil.find(:all) and send a json response to the
browser, who shows a table of pupils and its attributes.
I want to include in this listing on the browser a field showing what
courses each pupil got.
I don`t know the fast/easy method to achieve this. Maybe a custom,
more complex, select statment, or a additional select with additional
json request.

If you have hints or examples, it you be very welcome.

Thank you!

On Nov 20, 8:02 am, Tom L. [email protected] wrote:

I have 3 tables:
pupils: id, name, born_date, sex
courses: id, course_name, description
pupilXcourse: pupil_id, course_id

Today I run a Pupil.find(:all) and send a json response to the
browser, who shows a table of pupils and its attributes.
I want to include in this listing on the browser a field showing what
courses each pupil got.

If you’re just using Rails’ to_json and you’ve got the appropriate
associations then you can just do pupils.to_json :include => :courses

Fred

How will this “:include” change the json response? Since there is a
list being selected, I need to parse it in the client side.
Im not using simple to_json, but to_ext_json (provided by the plugin: http://inside.glnetworks.de/2008/01/18/announcing-ext-scaffold-generator-plugin-for-rails/), but I think the "include" wont change.

On Nov 20, 6:49 am, Frederick C. [email protected]

On 20 Nov 2008, at 13:29, Tom L. wrote:

How will this “:include” change the json response? Since there is a

list being selected, I need to parse it in the client side.
Im not using simple to_json, but to_ext_json (provided by the plugin: http://inside.glnetworks.de/2008/01/18/announcing-ext-scaffold-generator-plugin-for-rails/) , but I think the "include" wont change.

http://api.rubyonrails.com/classes/ActiveRecord/Serialization.html#M001417

Frederick C. escreveu:
On 20 Nov 2008, at 13:29, Tom L. wrote:
  
How will this ":include" change the json response? Since there is a

list being selected, I need to parse it in the client side.
Im not using simple to_json, but to_ext_json (provided by the plugin: <a class="moz-txt-link-freetext" href="http://inside.glnetworks.de/2008/01/18/announcing-ext-scaffold-generator-plugin-for-rails/">http://inside.glnetworks.de/2008/01/18/announcing-ext-scaffold-generator-plugin-for-rails/</a>) , but I think the "include" wont change.

http://api.rubyonrails.com/classes/ActiveRecord/Serialization.html#M001417
  
great!
Sorry for didn`t look at this doc before, but in really it was not
clear even the fact that :include is a to_json option.
The things was confuse for me.
Now it`s clear.

Thank you, Cheung!


Tom L.


--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---