【发布时间】:2023-03-16 05:04:01
【问题描述】:
一所学校有很多课程。一门课程有很多部分。学生注册课程的一部分。我希望能够找到学校的所有学生。
Class School < ActiveRecord::Base
has_many :courses
has_many :sections, :through => courses
has_many :students, :through => courses, :through => sections, :through => enrollments
end
Class Course < ActiveRecord::Base
belongs_to :school
has_many :sections
has_many :students, :through => sections, :through => enrollment
end
Class Section < ActiveRecord::Base
belongs_to :course
has_many :students, :through => enrollment
end
Class Student < ActiveRecord::Base
has_many :sections, :through => enrollment
has_many :courses, :through => sections, :through => enrollment
has_many :schools, :through => courses, :through => sections, :through => enrollment
end
enrollment 只是一个表格,其中包含学生在课程的该部分注册时的部分 ID 和学生 ID。
有没有更好的方法来做我想做的事情?
谢谢。
【问题讨论】:
标签: ruby-on-rails rails-models rails-3.1