Charles Bedrosian
2011-Jan-22 18:23 UTC
rendering json for a child model with multiple parent models
I am trying to render json for a child model that belongs_to two parent
models. The models represent a college course offering. Given a course,
I want to show what sections (instances of a course) are offered, and
during what terms (ie semester)
Model:
Course
has_many sections
Term
has_many sections
Section
belongs_to course
belongs_to section
I want to render the single course''s details along with all sections.
As
such:
render :json => @course.to_json(:include => :sections)
However, I would also like to include the term data above sections as a
means to group the sections.
courseA
term1
section_a
section_b
term2
section_c
section_d
Course is aware of Term only through Section
How can I go about doing this in the controller or the model? Can I
accomplish this through to_json or must I override to_json? Perhaps my
model associations need to be adjusted?
Thanks in advance!
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.