Rails (RoR) ActiveRecord Active Validation

I am trying to extend ActiveRecord with a special validate_as_email validation method, so I could use it like this:

class User < ActiveRecord::Base
  validates_as_email :email
end

I found a description of how to extend the ActiveRecord :: Base class: http://guides.rubyonrails.org/activerecord_validations_callbacks.html It says that you must create a * .rb class in config / initializers / {myfile} .rb.

ActiveRecord::Base.class_eval do  
   def self.validates_as_email(attr_name)  
      validate is_email_fn(attr_name)
   end
end 

What should I do next so that I can use validates_as_email in my model and where should I put the is_email_fn () function.

+1
source share
1 answer
+4
source

All Articles