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.


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

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

There’s also validates_each.

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

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