Skip to content

Instantly share code, notes, and snippets.

@ldenman
Created Oct 6, 2009
Embed
What would you like to do?
Common Validations
# app/models/student.rb
class Student < ActiveRecord::Base
include CommonValidations::StudentTeacherValidations
end
# app/models/teacher.rb
class Teacher < ActiveRecord::Base
include CommonValidations::StudentTeacherValidations
end
# lib/common_validations/student_teacher_validations.rb
module CommonValidations
module StudentTeacherValidations
def self.included(klass)
klass.validate :foo
end
def foo
class_name = self.class.class_name
errors.add("name", "must be the class name: #{class_name}") unless name == class_name
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment