Re: Is it possible to share validations between models?

you could try this:

module ContactValidations

def self.included(base_class) #include the new methods into this class base_class.extend(ClassMethods)

base_class.class_eval do #run the new code in the context of the class it's included. validates_presence_of(:name, :message => 'You must provide a company name.') validates_presence_of(:street, :message => 'You must provide a company street.') validates_presence_of(:city, :message => 'You must provide a company city.') validates_presence_of(:post_code, :message => 'You must provide a company post code.')

validates_numericality_of(:telephone, :on => :create, :message => 'Telephone should be a number with no spaces.', :if => :telephone_given?)

validates_numericality_of(:area_code, :on => :create, :message => 'Telephone area code should be a number with no spaces.', :if => :area_code_given?)

validates_numericality_of(:fax, :on => :create, :message => 'Fax should be a number with no spaces.', :if => :fax_given?)

validates_numericality_of(:fax_area_code, :on => :create, :message => 'Fax area code should be a number with no spaces.', :if => :fax_area_code_given?)

Re: Is it possible to share validations between models?

Basically what duplex said but you want to include your instance methods:

require 'active_record'

module ContactValidations

def self.included(base_class) base_class.class_eval do include ContactValidations::InstanceMethods

# model validations validates_presence_of(:name, :message => 'You must provide a company name.') validates_presence_of(:street, :message => 'You must provide a company street.') validates_presence_of(:city, :message => 'You must provide a company city.') validates_presence_of(:post_code, :message => 'You must provide a company post code.')

validates_numericality_of(:telephone, :on => :create, :message => 'Telephone should be a number with no spaces.', :if => :telephone_given?) validates_numericality_of(:area_code, :on => :create, :message => 'Telephone area code should be a number with no spaces.', :if => :area_code_given?)

validates_numericality_of(:fax, :on => :create, :message => 'Fax should be a number with no spaces.', :if => :fax_given?)

validates_numericality_of(:fax_area_code, :on => :create, :message => 'Fax area code should be a number with no spaces.', :if => :fax_area_code_given?)