|
2 | 2 |
|
3 | 3 | class SchoolClass < ApplicationRecord |
4 | 4 | belongs_to :school |
5 | | - has_many :members, class_name: :ClassMember, inverse_of: :school_class, dependent: :destroy |
| 5 | + has_many :students, class_name: :ClassStudent, inverse_of: :school_class, dependent: :destroy |
| 6 | + has_many :class_teachers, class_name: :ClassTeacher, inverse_of: :school_class, dependent: :destroy |
6 | 7 | has_many :lessons, dependent: :nullify |
| 8 | + accepts_nested_attributes_for :class_teachers |
7 | 9 |
|
8 | | - validates :teacher_id, presence: true |
| 10 | + scope :with_class_teacher, ->(user_id) { joins(:class_teachers).where(class_teachers: { id: user_id }) } |
| 11 | + |
| 12 | + # validates :teacher_id, presence: true |
9 | 13 | validates :name, presence: true |
10 | | - validate :teacher_has_the_school_teacher_role_for_the_school |
| 14 | + # validate :teacher_has_the_school_teacher_role_for_the_school |
11 | 15 |
|
12 | 16 | def self.teachers |
13 | | - User.from_userinfo(ids: pluck(:teacher_id)) |
| 17 | + User.from_userinfo(ids: ClassTeacher.pluck(:teacher_id)) |
14 | 18 | end |
15 | 19 |
|
16 | 20 | def self.with_teachers |
17 | 21 | by_id = teachers.index_by(&:id) |
18 | | - all.map { |instance| [instance, by_id[instance.teacher_id]] } |
| 22 | + all.map { |instance| [instance, instance.teacher_ids.map { |teacher_id| by_id[teacher_id] }] } |
| 23 | + end |
| 24 | + |
| 25 | + def teacher_ids |
| 26 | + class_teachers.pluck(:teacher_id) |
19 | 27 | end |
20 | 28 |
|
21 | | - def with_teacher |
22 | | - [self, User.from_userinfo(ids: teacher_id).first] |
| 29 | + def with_teachers |
| 30 | + [self, User.from_userinfo(ids: teacher_ids)] |
23 | 31 | end |
24 | 32 |
|
25 | | - private |
| 33 | + # private |
26 | 34 |
|
27 | | - def teacher_has_the_school_teacher_role_for_the_school |
28 | | - return unless teacher_id_changed? && errors.blank? |
| 35 | + # def teacher_has_the_school_teacher_role_for_the_school |
| 36 | + # return unless teacher_id_changed? && errors.blank? |
29 | 37 |
|
30 | | - user = User.new(id: teacher_id) |
31 | | - return if user.school_teacher?(school) |
| 38 | + # user = User.new(id: teacher_id) |
| 39 | + # return if user.school_teacher?(school) |
32 | 40 |
|
33 | | - msg = "'#{teacher_id}' does not have the 'school-teacher' role for organisation '#{school.id}'" |
34 | | - errors.add(:user, msg) |
35 | | - end |
| 41 | + # msg = "'#{teacher_id}' does not have the 'school-teacher' role for organisation '#{school.id}'" |
| 42 | + # errors.add(:user, msg) |
| 43 | + # end |
36 | 44 | end |
0 commit comments