Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

validates_presence_of in a module

i have a model. i want import in this model a module. in this module i want insert a validates_presence_of for the models that import it

I want know if and how is possible to do something like this:

class Ele < ActiveRecord::Base
  include Mod
end

module Mod
   validates_presence_of     :field
end

Thanks

like image 733
Luca Romagnoli Avatar asked Feb 27 '23 14:02

Luca Romagnoli


1 Answers

You can use the self.included hook.

class Ele < ActiveRecord::Base
  include Mod
end

module Mod
  def self.included(base)
    base.class_eval do
      validates_presence_of :field
    end
  end
end
like image 194
Simone Carletti Avatar answered Mar 07 '23 16:03

Simone Carletti