1

I have 4 database tables in Postgres. course, activity, lesson and task.

Currently when I do Course.find_by_id(params[:id])in my controller the models are setup to return all lessons in a course, but I'd like this nesting to extend to the activities in the lesson and include the one-to-one relationship of task to activity too.

class Course < ActiveRecord::Base
  validates :title, uniqueness: { case_sensitive: false }, presence: true

  has_many :lessons, -> { order(position: :asc) }, dependent: :restrict_with_error
  belongs_to :subject

  accepts_nested_attributes_for :lessons
end


class Lesson < ActiveRecord::Base
  validates :title, uniqueness: { case_sensitive: false }, presence: true
  validates :position, uniqueness: { scope: :course_id }, presence: true

  belongs_to :course
  has_many :activities, dependent: :restrict_with_error
  acts_as_list column: :position, scope: :course

  accepts_nested_attributes_for :course
end


class Activity < ActiveRecord::Base
  validates :name, uniqueness: { case_sensitive: false }, presence: true
  validates :task, presence: true
  validates :term, presence: true

  belongs_to :lesson
  belongs_to :term
  has_one :task, dependent: :destroy

  accepts_nested_attributes_for :task, allow_destroy: true

  attr_accessor :taskable_attributes
end

class Task < ActiveRecord::Base
  validates :name, uniqueness: { case_sensitive: false }, presence: true
  validates :taskable, presence: true

  belongs_to :activity
  belongs_to :taskable, polymorphic: true, dependent: :destroy

  accepts_nested_attributes_for :taskable, allow_destroy: true
end

My JSON output currently looks like this:

{
    "id": 1,
    "title": "Algebra",
    "description": "Do you know your y times table?",
    "subject_id": 1,
    "lessons": [
        {
            "id": 1,
            "title": "Functions",
            "description": "A mapping of inputs to outputs",
            "position": 2,
            "course_id": 1
        }
    ]
}

But I would like it to contain more nested data

{
    "id": 1,
    "title": "Algebra",
    "description": "Do you know your y times table?",
    "subject_id": 1,
    "lessons": [
        {
            "id": 1,
            "title": "Functions",
            "description": "A mapping of inputs to outputs",
            "position": 2,
            "course_id": 1,
            "activities": [
                {
                   "id": 1,
                   "title": "Activity 1"
                   "task": 
                       {
                           "id": 1,
                           "name": "Task name here"
                       }
               }
            ]
        }
    ]
}

I've never used ruby or ruby on rails before, but being able to add this nesting to the frontend app until our rails developer returns would make my job a lot easier.

1 Answer 1

4

you can try in your responsible controller something like:

render json: @course.to_json( { include: [ lessons: { include: { :task } } ] } )

You can also exclude fields from any association with only, also you can include some methods with methods:{}

Cheers

Sign up to request clarification or add additional context in comments.

1 Comment

My final solution was: render json: Course.all.to_json( { include: [ lessons: { include: [ activities: { include: :task } ] } ] } )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.