I understand that the documentation to the Rails 3.0.0 validates method is in ActiveModel::Validations::ClassMethods
I was wondering then, how a class that inherits from ActiveRecord::Base has the validates method?
For example, it is common to use validates in such a way:
class User < ActiveRecord::Base
attr_accessible :name, :email
validates :name, :presence => true
end
I looked at the class hierarchy and it seems like SomeModelClass extends ActiveRecord::Base extends Object
How is a method in Active::Validations::ClassMethods available to the model object that inherits from ActiveRecord::Base?
Thanks a lot for your help!
ActiveRecord::Base calls
Base.class_eval { include Validation }(and a bunch of other includes as well), which makes the methods inside ActiveRecord::Validations available as class macros.Here’s the actual line: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/base.rb#L306
You’ll see this kind of thing all over Rails.