Ruby Active Record Validations Intro
Tags: beginners, webdev, ruby, codenewbie
Published: 2022-10-25 19:39:58

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. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gyf08ezyzijekz3w90k5.png)

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

```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ewwkjsvn57i6lo67s9nt.jpg)
_Behold a Hatdog! (Photo by <a href="https://unsplash.com/@meghankix?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Meghan Hessler</a> on <a href="https://unsplash.com/s/photos/dog-hat?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>)_

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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/28wggpujv707wje5ov4b.jpg)
_Definitely not too old! (Photo by <a href="https://unsplash.com/@mandacreatespretty?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Manda Hansen</a> on <a href="https://unsplash.com/s/photos/dog-hat?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>)_

**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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f84g6nxyg6z1tu0wnxei.jpg)
_Catchphrase: "I let the dogs out" (Photo by <a href="https://unsplash.com/@jamie452?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Jamie Street</a> on <a href="https://unsplash.com/s/photos/dog-hat?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>)_

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!