进行以下设置:
@ActivityScope
我想确认在该学生已经上过课的时期不能入学。
目前,我正在开设有关课程和教学的期间专栏文章。
class Faculty < ApplicationRecord
has_many :courses
has_many :courses, :through => :teachings
end
class Teaching < ApplicationRecord
belongs_to :faculty
belongs_to :course
end
class Course < ApplicationRecord
belongs_to :faculty
has_many :registrations
has_many :students, through: :registrations
has_many :teachings
has_many :faculty, through: :teachings
validates :name, uniqueness: { scope: [:faculty_id, :period,
:semester, :year] }
end
class Registration < ApplicationRecord
belongs_to :student
belongs_to :course
validates_uniqueness_of :student_id, :scope => :course_id
validates_uniqueness_of :student_id, :scope => :period
end
class Student < ApplicationRecord
has_many :registrations
has_many :courses, :through => :registrations
validates :email, uniqueness: true
end
这显然不是一个很好的解决方案。我的其他解决方案似乎同样令人费解,在创建记录之前,我将检查所有已注册学生的班级,并确认新班级不是已经上课了。
我该如何设计它以使其在任何时期都不能重复预订学生(或与此相关的老师)?
编辑:
我最终要做的是:
validates_uniqueness_of :student_id, :scope => :course_id
validates_uniqueness_of :student_id, :scope => :period
如果这是正确的方法,我仍将不胜感激