问题:在 enrollment.attendances.count(:id)
中,注册必须只能访问与 student_id 和 course_id 匹配的那些出勤率。
这是我的 4 db 依赖项:
class Enrollment < ActiveRecord::Base
belongs_to :course
belongs_to :student
has_many :attendances, through: :client
has_many :attendances, through: :course
,
class Student < ActiveRecord::Base
has_many :enrollments
has_many :attendances
,
class Course < ActiveRecord::Base
has_many :enrollments
has_many :attendances
,
class Attendance < ActiveRecord::Base
belongs_to :student
belongs_to :course
,
最佳答案
首先可以通过student_id
和course_id
从Enrollment
中找到object
然后检查是否出席
然后你可以找到count
。
@enrolls = Enrollment.where(course_id: id1, student_id: id2)
@enrolls.each do |enrol|
if enrol.attended == true
puts Attendance.where(course_id: id1, student_id: id2).count
end
end
关于ruby-on-rails - ruby 有很多 through 两种模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37386196/