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!