course.rb 503 B

123456789101112131415161718192021
  1. class Course < ApplicationRecord
  2. belongs_to :school
  3. validates :school_id, presence: true
  4. validates :name, presence: true, length: { maximum: 255 }
  5. validates :starts_on, presence: true
  6. validates :ends_on, presence: true
  7. validate :date_order
  8. has_many :course_participations, dependent: :destroy
  9. has_many :users, through: :course_participations
  10. private
  11. def date_order
  12. if ends_on < starts_on
  13. errors.add(:ends_on, "should come after starts_on")
  14. end
  15. end
  16. end