ActiveRecord从DB返回嵌套的JSON数据

时间:2022-05-14 16:32:11

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

我在Postgres有4个数据库表。课程,活动,课程和任务。

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.

目前,当我在我的控制器中执行Course.find_by_id(params [:id])时,模型被设置为返回课程中的所有课程,但我希望这个嵌套扩展到课程中的活动并包括一个 - - 任务与活动的关系。

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:

我的JSON输出目前看起来像这样:

{
    "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.

我之前从未在轨道上使用过ruby或ruby,但能够将这个嵌套添加到前端应用程序,直到我们的rails开发人员返回将使我的工作变得更容易。

1 个解决方案

#1


1  

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

干杯

#1


1  

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

干杯