Extend the ActiveRecord::Base

activerecord, ruby-on-rails

Solution

- config/initializers/whatever.rb

- nope

- nope ... initializers are loaded on application boot

- nope

- Yup. Rails autoload will search for it.

The rails-ish way of doing what is you're trying to do is: create a file in lib/encryptable.rb (or app/models/concerns if you're on rails 4) which defines a module with your methods. Then in your models you can do `include Encryptable` or (for all models) in an initializer:

ActiveRecord::Base.class_eval do
  include Encryptable
end

read more about rails 4 concerns here: How to use concerns in Rails 4

Problem

If I make 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 do I have to save this file? - Do it need to have a special name? - Do I have to call `require` somewhere? - Could I save it in the model folder? - Is a class declared in the model folder visible from the other classes in the model folder without calling `require`?

Original source