ActiveRecord Validations
ActiveRecord Validations
ActiveRecord can validate our models for us before they even touch the database. This means it's harder to end up with bad data, which can cause problems later even if our code is technically bug-free.
We can use ActiveRecord::Base
helper methods like #validates
to set things up.
Objectives
After this lesson, you should be able to:
Identify when validation occurs in the lifespan of an object
Introspect on the
ActiveRecord::Errors
collection object on an AR instanceuse
#valid?
use
#errors
Generate
full_message
s for errorsCheck an attribute for validation errors
Add a custom validation error to an AR model
Context: Databases and Data Validity
What is a "validation"?
In the context of Rails, validations are special method calls that go at the top of model class definitions and prevent them from being saved to the database if their data doesn't look right.
In general, "validations" are any code that perform the job of protecting the database from invalid code.
AR Validations Are Not Database Validations
Many relational databases such as SQLite have data validation features that check things like length and data type. In Rails, these validations are not used, because each database works a little differently, and handling everything in ActiveRecord itself guarantees that we'll always get the same features no matter what database we're using under the hood.
What is "invalid data"?
Suppose you get a new phone and you ask all of your friends for their phone number again. One of them tells you, "555-868-902". If you're paying attention, you'll probably wrinkle your nose and think, "Wait a minute. That doesn't sound like a real phone number."
"555-868-902" is an example of invalid data... for a phone number. It's probably a valid account number for some internet service provider in Alaska, but there's no way to figure out what your friend's phone number is from those nine numbers. It's a showstopper, and even worse, it kind of looks like valid data if you're not looking closely.
Validations Protect the Database
Invalid data is the bogeyman of web applications: it hides in your database until the worst possible moment, then jumps out and ruins everything by causing confusing errors.
Imagine the phone number above being saved to the database in an application that makes automatic calls using the Twilio API. When your system tries to call this number, there will be an error because no such phone number exists, which means you need to have an entire branch of code dedicated to handling just that edge case.
It would be much easier if you never have bad data in the first place, so you can focus on handling edge cases that are truly unpredictable.
That's where validations come in.
Basic Usage
#validates
is our Swiss Army knife for validations. It takes two arguments: the first is the name of the attribute we want to validate, and the second is a hash of options that will include the details of how to validate it.
In this example, the options hash is { presence: true }
, which implements the most basic form of validation, preventing the object from being saved if its name
attribute is empty.
Lifecycle Timing
Before proceeding, keep the answer to this question in mind:
What is the difference between #new
and #create
?
If you've forgotten, #new
instantiates a new ActiveRecord model without saving it to the database, whereas #create
immediately attempts to save it, as if you had called #new
and then #save
.
Database activity triggers validation. An ActiveRecord model instantiated with #new
will not be validated, because no attempt to write to the database was made. Validations won't run unless you call a method that actually hits the DB, like #save
.
The only way to trigger validation without touching the database is to call the #valid?
method.
Validation Failure
Here it is, the moment of truth. What can we do when a record fails validation?
How can you tell when a record fails validation?
Pay attention to return values!
By default, ActiveRecord does not raise an exception when validation fails. DB operation methods (such as #save
) will simply return false
and decline to update the database.
Every database method has a sister method with a !
at the end which will raise an exception (#create!
instead of #create
and so on).
And of course, you can always check manually with #valid?
.
Finding out why validations failed
To find out what went wrong, you can look at the model's #errors
object.
You can check all errors at once by examining errors.messages
.
You can check one attribute at a time by passing the name to errors
as a key, like so:
Displaying Validation Errors in Views
This constructs more complete-looking sentences from the more terse messages available in errors.messages
.
Other Built-in Validators
Rails has a host of built-in helpers.
Length
length
is one of the most versatile:
Remember that there's no syntactical magic happening with any of these method calls. If we weren't using Ruby's "poetry mode" (which is considered standard for Rails), the above code would look like this:
Phew!
Uniqueness
Another common built-in validator is uniqueness
:
This will prevent any account from being created with the same email as another already-existing account.
Custom Messages
This isn't a validator in its own right, but a handy convenience option for specifying your own error messages:
Custom Validators
Of the three, #validate
is the simplest. If your validation needs become more complex, consult the documentation. For most validations, though, the following method should be good enough.
Create a new directory in
app
calledvalidators
. Because most Rails developers don't need to write custom validation, this directory is not created by default likemodels
orcontrollers
.Identify the ActiveRecord attribute you want to validate. Is it the
email
or thelast_name
on thePerson
class, for example?Create a new file in the
app/validators
directory of the form attribute (from the previous step) +_validator.rb
. So in the case of validating an attribute calledemail
, create a fileapp/validators/email_validator.rb
Inside the new file, define the class. The class name should match the file name of the file, but "Camel-Cased." So
email_validator
should be classEmailValidator
. The class should inherit fromActiveModel::Validator
The validator class must have one instance method,
#validate
. This method will receive one argument typically calledrecord
.Inside of
#validate
, you'll be able to get properties fromrecord
and determine whether it is invalid. If the record is invalid, push (<<
) torecord.errors[:attribute]
e.g.record.errors[:email]
aString
which is a message that you want to display that describes why the message is not valid.Lastly, in the implementation of the class being validated e.g.
Person
, add:An
include
of ActiveModel::ValidationsThe helper call:
validates_with (className)
. In our example we'd put,validates_with EmailValidator
(see step 4, above)
The result of these steps should be the following:
Here we validate that all email addresses are in the flatironschool.com
domain.
Last updated