Extend ActiveRecord :: Base

If I do something like this:

class ActiveRecord::Base
  def self.encrypt(*attr_names)
    encrypter = Encrypter.new(attr_names)

    before_save encrypter
    after_save  encrypter
    after_find  encrypter

    define_method(:after_find) { }
  end
end
  • Where should I save this file?
  • Do I need to have a special name?
  • Do I need to call requiresomewhere?
  • Can I save it in the model folder?
  • A class declared in the model folder visible from other classes in the model folder without a call require?
+4
source share
1 answer
  • config / Initializers / whatever.rb
  • No
  • nope ... initializers load when the application loads
  • No
  • Yeah. Rails autoload will look for it.

- , : lib/encryptable.rb( app/models/problems, 4), . include Encryptable ( ) :

ActiveRecord::Base.class_eval do
  include Encryptable
end

4: Rails 4

+5

All Articles