UPDATE (December 4, 2010):
I realized that each validates line is actually a method call (obviously), so requiring them like this was not quite the way I expected.
This works, but I'm not sure if it is right (fully qualify the Auction class name):
class Auction::Validations Auction.validates :status, :presence => true, :inclusion => { :in => [ Auction::CREATING, Auction::OPEN, Auction::PENDING, Auction::CANCELLED, Auction::SUSPENDED, Auction::EXPIRED, Auction::CLOSING_COMPLETED, Auction::CLOSING_WON, Auction::COMPLETED, Auction::WON, Auction::NEGOTIATING, Auction::IN_ESCROW ] } Auction.validates :user, :presence => true Auction.validates :url, :presence => true, # FIXME: Move this to a URLValidator and do :url => true :format => /^https?:\/\/[a-z0-9-]+(\.[a-z0-9-])*\.[a-z0-9]+\/.*/i Auction.validates :title, :presence => true, :length => { :maximum => 255 } Auction.validates :description, :presence => true Auction.validates :reserve, :numericality => { :greater_than_or_equal_to => :minimum_bid } end
If necessary ( require 'auction/validations ) in the Auction class, it does the right thing.
The original question follows:
Several of my model classes clutter up all of these βchecksβ a bit, so I thought I could move them to a separate class and βrequireβ, but that doesn't seem to work.
class Auction < ActiveRecord::Base require 'auction/validations' ... class Auction::Validations include ActiveModel::Validations validates :status, :presence => true, :inclusion => { :in => [ ... snip ... ] } validates :user, :presence => true validates :url, :presence => true,
This is not an error, but validates_each does not execute the block at all (verified by adding puts "here" ), and checking the numerical value no longer works.
With the body of this class, which is blindly copied back to the Auction class, everything will work.
Do I really not understand what will require these validations?
EDIT:
In fact, none of the validations work. Not just these two. Hmmm.