Custom validation in activerecord

I looked all over last night trying to find the best way to create
custom validations in an activerecord class. Anyone have a link or
example lying around? The custom validation can’t be done with a
regular expression.

Chris

You have a few options. Here’s one way:

def validate
errors.add :first_name, “can’t be admin” if self.first_name ==
“admin”
errors.add :notes, “must not contain more than one URL” if find_spam(
self.notes)
end

There’s also validates_each.

See the api for this. It sure could be documented better but there are a
few
examples.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs