Andy
Andy

Reputation: 751

validate a rails activerecord field by a callback method?

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?

Upvotes: 0

Views: 80

Answers (1)

Austin
Austin

Reputation: 3890

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.

Upvotes: 1

Related Questions