I have 4 models: SchoolClass, Timetable, Lesson, Reporting:
# class_code :string(255)
class SchoolClass < ActiveRecord::Base
has_many :timetables
end
class Timetable < ActiveRecord::Base
belongs_to :school_class
has_many :lessons
end
class Lesson < ActiveRecord::Base
belongs_to :timetable
has_one :reporting
end
# report_type :string(255)
class Reporting < ActiveRecord::Base
belongs_to :lesson
validates :report_type,
:presence => true,
:inclusion => { :in => %w(homework checkpoint)}
end
How can i validate that each SchoolClass can have only 1 Reporting with type “checkpoint“?
This gets super complicated because of the nesting associations.
I would start by using a custom validation method.
in the SchoolClass model:
and then the method:
There, I think that does it. If I understand your problem correctly.