Active Record Validations

Creating and saving a new record will send an SQL INSERT operation to the database. Updating an existing record will send an SQL UPDATE operation instead. Validations are typically run before these commands are sent to the database. If any validations fail, the object will be marked as invalid and Active Record will not perform the INSERT or UPDATE operation. This avoids storing an invalid object in the database. You can choose to have specific validations run when an object is created, saved, or updated.

 

Note that save also has the ability to skip validations if passed validate:
false
as an argument. This technique should be used with caution.

  • save(validate: false)

 

 

There are a few ways to skip the validations. For example, we have two attributes to update. Here we use two calls to update_attribute rather than a single call to updated_attributes because the latter would run the validations. Lacking in this case the user password, these validations would fail.