validate a rails activerecord field by a callback method?

Go To StackoverFlow.com

0

is there a way that can validate a single field from the boolean returned by a callback method? something like:

validate_with_callback :title, :callback_method, 'an error message'

doesn't have to be exactly the same, but is there something similar built-in in rails?

2012-04-04 04:27
by Andy


1

I suppose you could do something like:

def self.validate_with_callback(attr, method, error)
  validate do |record|
    record.errors.add attr, error unless record.send(method)
  end
end

But this is just a dumb wrapper around validate, so I'd just call it directly.

2012-04-04 04:48
by Austin
Ads