轨道模型学科和学生

时间:2016-09-09 06:21:33

标签: ruby-on-rails

我现在有四个模特我有老师,学生,科目和学生和科目的连接表,这是学生主题

对于我的老师模特

class Teacher < ApplicationRecord
 has_many :subjects, dependent: :destroy
 has_many :students, dependent: :destroy

 validates :firstname, uniqueness: {scope: :lastname}, presence: true
 validates :lastname, presence: true
end

对于我的学生模特

class Student < ApplicationRecord
 belongs_to :teacher
 has_many :student_subjects, dependent: :destroy
 has_many :subjects, through: :student_subjects

 validates :first_name, :last_name, uniqueness: {scope: :teacher_id},presence: true
end

对于我的主题模型

class Subject < ApplicationRecord
 belongs_to :teacher
 has_many :student_subjects, dependent: :destroy
 has_many :students, through: :student_subjects

 validates :title, uniqueness: {scope: :teacher_id}, presence: true
 validates :code, presence: true
end

对于我的学生主题模型

class StudentSubject < ApplicationRecord
 belongs_to :student
 belongs_to :subject
 has_many :grades, dependent: :destroy

 validates :student_id, uniqueness: {scope: :subject_id }, presence: true
end

这里是我的问题我想验证我的学生主题模型,如果学生有相同的主题,这意味着学生没有重复主题

0 个答案:

没有答案
相关问题