Given a model
class BaseModel < ActiveRecord::Base
validates_presence_of :parent_id
before_save :frobnicate_widgets
end
and a derived model (the underlying database table has a type
field - this is simple rails STI)
class DerivedModel < BaseModel
end
DerivedModel
will in good OO fashion inherit all the behaviour from BaseModel
, including the validates_presence_of :parent_id
. I would like to turn the validation off for DerivedModel
, and prevent the callback methods from firing, preferably without modifying or otherwise breaking BaseModel
What's the easiest and most robust way to do this?
I like to use the following pattern:
class Parent < ActiveRecord::Base
validate_uniqueness_of :column_name, :if => :validate_uniqueness_of_column_name?
def validate_uniqueness_of_column_name?
true
end
end
class Child < Parent
def validate_uniqueness_of_column_name?
false
end
end
It would be nice if rails provided a skip_validation method to get around this, but this pattern works and handles complex interactions well.