Ruby Active Record Validations Intro
logan-lampton
Posted on October 25, 2022
It's a powerful feeling once we have our database up and running and able to take the data provided by our users and persistently save that data on the backend. But how can we make sure that the user provides the data that we need and only in a format that makes sense for comparing with the rest of the information in our database? For example, we want to make sure that users provide a username and password when signing up for an account on our website and can't just leave the password input blank. That's where validations come in!
First let's take a look at where validations are located in Ruby active record. You'll be adding any validations you create to the app/models file.
In our example, we are creating a very important database for dogs wearing hats, so we will add our validations to the file: app/models/hatdogs which is currently blank:
class Hatdog < ApplicationRecord
end
Behold a Hatdog! (Photo by Meghan Hessler on Unsplash)
Before we add our validations, we'll want to take a look at what kind of data we are looking for. This can be found in our schema or Hatdog migration.
class CreateHatdogs < ActiveRecord::Migration[7.0]
def change
create_table :hatdogs do |t|
t.string :name
t.integer :age
t.string :image
t.string :catchphrase
t.timestamps
end
end
end
We want a name, age, image and catchphrase for each object. All of these will have the type of string, except for age. For each validation, we will use the "validates" keyword.
Presence
If we want to make sure that when users go to add a new Hatdog on the front-end, that they provide a name, age and image, we can easily do so with the presence: true validation, like so.
class Hatdog < ApplicationRecord
validates :name, presence: true
validates :age, presence: true
validates :image, presence: true
end
Numericality
We'd like for users to only use integers for age, so that we stop getting things like "Old!" submitted to our database. We can use numericality for that. Let's add it to our current validation for :age.
class Hatdog < ApplicationRecord
validates :name, presence: true
validates :age, presence: true, numericality: { only_integer: true }
validates :image, presence: true
end
Definitely not too old! (Photo by Manda Hansen on Unsplash)
greater_than_or_equal_to and less_than_or_equal_to
Let's add some more validations to age, so that we don't get crazy numbers like -1 and 1000.
class Hatdog < ApplicationRecord
validates :name, presence: true
validates :age, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 25 }
validates :image, presence: true
end
Uniqueness
Let's say we want each picture to be unique to prove we have different Hatdogs. (We're not going down the rabbit hole of hoping that each dog name is completely unique!) To make sure the user input is unique, we'll use uniqueness.
class Hatdog < ApplicationRecord
validates :name, presence: true
validates :age, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 25 }
validates :image, presence: true, uniqueness: true
end
Length
Let's add a validation for the catchphrase! We don't want users submitting an essay, so let's limit how long the catchphrase can be to a maximum of 150 characters.
class Hatdog < ApplicationRecord
validates :name, presence: true
validates :age, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 25 }
validates :image, presence: true, uniqueness: true
validates :catchphrase, length: { maximum: 150 }
end
Catchphrase: "I let the dogs out" (Photo by Jamie Street on Unsplash)
This is just the beginning of all the helpful ways that validations can help you keep your data clean and useful. For more information to help with your continued development, please take a look at the official Ruby documentation. It's great (if a little lacking in dog images) read: https://guides.rubyonrails.org/active_record_validations.html
Happy coding!
Posted on October 25, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.