Hey all!
Hope you’re having a good weekend.
I have a couple of models that share a lot of similar validation so I
figured this would be a good opportunity to DRY my code up.
lets say for example:
class Squirrel < ActiveRecode::Base
validates_presence_of :name
end
class Badger < ActiveRecode::Base
validates_presence_of :name
end
what I’ve been trying to do is add a module in /lib called
CommonValidations
module CommonValidations
def included(base)
base.class_eval do
validates_presence_of :name
end
end
end
and then include this module in the models that require it. I’ve also
tried various combinations with ‘eval’ and ‘send’ etc.
The validations aren’t called
Can anyone explain why this isn’t working and if it’s possbile to re-
use validations like this?
Thanks
Gavin