【发布时间】:2014-10-04 20:54:08
【问题描述】:
所以这个问题难倒了我。我有以下基本模型结构;
class Student < ActiveRecord::Base
has_many :attendances
has_many :classes, :through => :attendances
end
class Attendance < ActiveRecord::Base
belongs_to :student
validates :student_id, presence: true
belongs_to :class
validates :class_id, presence: true
end
class Class < ActiveRecord::Base
has_many :attendances
has_many :students, :through => :attendances
end
我正在尝试构建一个针对学生(当前登录用户)的简单应用,我们会显示他们在给定班级的平均出勤率,以及该班级所有学生的平均出勤率。
出勤记录仅包含 student_id、class_id 和出勤率(当前为整数)。
所以一个真实的例子是;
生物学学生出勤率 - 80 生物学的学生平均出勤率 - 96
我的尝试
我试图在 Student 控制器的 Show 方面定义 @students = Student.all,并需要出勤和班级的参数,在我看来运行 @students.attendance.average('attended') - {其中“参加”是保存整数的列'} - 但我收到一个错误,即“出席”未定义。
我哪里错了?
谢谢大家
【问题讨论】:
标签: ruby-on-rails activerecord