ActiveRecord Validations.

Luis Dejesus Castro
3 min readAug 5, 2021

--

While building MVC applications with Ruby on Rails, you may have heard of or even worked with ActiveRecord Validations. For those of you that haven’t, you can read more about them and their application when building a back-end here.

Why are Validations Important to Development?

Validations are a useful tool for developers because it allows us the ability to set rules for the attributes inside an object which prevents us from persisting bad data. As of writing this, I am currently developing a dog-walking app. The app allows users to sign up as a Client or as a Walker, where Clients have the ability to keep a record of dogs and make posts looking for Walkers, while Walkers have the ability to view posts by Clients as well as review dogs associated with a client to make the decision of whether or not to accept a job. I will use the models within my app to adequately represent validations and the abilities they give us.

In the Model:

Within my app, I have a Dog Model that establishes the active record association between a Dog and Client but also validates the name, age, breed, and sex of a given dog. If you are following along for your own personal project, the terminal command to create a rails model is:

rails g model <model name>

Notice that the only validation I have for the listed attributes checks for presence, meaning before persisting this instance to a database, these input fields are validated to see if they have been filled out and have not been left blank. For a detailed list of validation helpers, click here.

Once your validations are defined in your model, you are free to run your migrations to create the necessary tables within your database if you haven’t already. Note: you can still add or edit validations even after your database has been created. You do NOT need to roll back migrations for validations to take effect.

In the Controller:

Since we are working with MVC development, if we have a Dog Model we should also have a DogsController. To create a controller from the terminal, use the command:

rails g controller <controller name>

Within the Dogs Controller, I know I'm going to want a create action to handle creating an instance of a dog using dog_params, but also make sure the information entered is valid using the .valid? which checks for errors on the object. The way I set up my error response handler, any attribute that doesn’t pass it’s validations will trigger a message response to the errors hash. I will write another blog on error response handling seperate from this blog.

the reason I have a commented-out HTTP verb and route above the controller action is for organizational purposes. It allows me to keep track of the routes that I will eventually add to the routes.rb file in the config folder. Likewise, if a certain route is not working as intended, I know which controller action to look for. I am a very new developer and these small things help me stay organized.

In my create action, the DogsController checks to see if there is a :client_id signed into the sessions hash and and if there is, uses Active Record Associations to create a dog associated to the client using the params defined in dog_params. We haven’t defined dog_params yet so think of it as a place holder for the code you wish you had.

dog_params (Strong Params):

If you have never heard of strong params think of it as “the list” for a popular nightclub, where the bouncer represents the the params, “the list” represents all the attributes listed in the params, and you represent the object getting sent through a request. If your names not on the list, you’re not getting in. Which means if an object does not have the correct type of data associated to the correct attributes, it will not be saved to the database.

Because each dog is associated with an instance of a client, you must include :client_id within the strong params to be able to create a new instance of a dog. Otherwise, the dog will not persist to the database.

NOTICE: There are some attributes listed in dog_params that were not given validations. This means that as long as a request is sent with the right attributes, no further validations will be run on those attributes.

Now that your create action is set up properly and the correct parameters are defined within dog_params, you are ready to allow for the creation of dogs within your app without the fear that users will persist bad data.

I will write another blog explaining error handling, how it relates to Active Record Validations and what that looks like on the front-end.

--

--