views:

17

answers:

1

Hi all,

I understand that the documentation to the Rails 3.0.0 validates method is in ActiveModel::Validations::ClassMethods http://www.railsapi.com/doc/rails-v3.0.0/classes/ActiveModel/Validations/ClassMethods.html#M003721

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!

+1  A: 

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: http://github.com/rails/rails/blob/master/activerecord/lib/active_record/base.rb#L1838

You'll see this kind of thing all over Rails.

rspeicher
thanks for your help!
Yink