I have a Rails model class like this:
class Something < ActiveRecord::Base
before_create do
self.key = SecureRandom.urlsafe_base64(8)
end
end
Why can I call before_create here? I expected it to be a method of ActiveRecord::Base but it is not. Callbacks are methods of ActiveRecord::Callbacks. But why can I call them in a model class without including something?
You can do that because
ActiveRecord::Basedoes this (or something similar depending on your version of Rails):So
ActiveRecord::Basealready includesActiveRecord::Callbacksand your class picks up the callbacks by inheriting fromActiveRecord::Base.