我有一个班级模特,一个学生模型和一个考勤模型。学生可以参加考勤以提高绩效。
我想显示班级中所有学生的数量,现在学生的数量,缺席学生的数量和出勤率。我是Mongodb的新手,我将不胜感激任何帮助。谢谢你的时间。
class Klass
include Mongoid::Document
include Mongoid::Timestamps
has_and_belongs_to_many :students
field :name, type: String
end
class Student
include Mongoid::Document
include Mongoid::Timestamps
has_and_belongs_to_many :klasses
embeds_many :attendances
field :name, type: String
end
class Attendance
include Mongoid::Document
include Mongoid::Timestamps
embedded_in :student
field :status, type: Integer # 1 = Present, 2 = Absent
field :klass_id, type: BSON::ObjectId
end
答案 0 :(得分:1)
你可以试试这些:
@class = Klass.where(name: 'something').first
@total_students = @class.students.count
@present_students = @class.students.where('attendances.status' => '1').count
@absent_students = @class.students.where('attendances.status' => '2').count
@p_s_today = @class.students.where('attendances.status' => '1', 'attendances.created_at' => {'$gte' => Date.today} ).count
@a_s_today = @class.students.where('attendances.status' => '2', 'attendances.created_at' => {'$gte' => Date.today} ).count
答案 1 :(得分:1)
我已经通过遵循技术解决了我的问题。
@students_present_today = @class.students.where({ attendances: { '$elemMatch' => {status: 1, :created_at.gte => Date.today} } }).count
@students_absent_today = @class.students.where({ attendances: { '$elemMatch' => {status: 2, :created_at.gte => Date.today} } }).count