Gaining Validation
Protecting an Application Database with Validations in Rails
Validations ensure that only valid data is saved into a database. These are important in order to protect the integrity of an application. Take for instance, a wine application; a wine may have the following attributes:
- A Title
- A Country (from which it originates)
- A Rating
- A year
- A Category
- A Price
What if we wanted to insure that all the above attributes were added to a wine instance before allowing it to save into our application database? This could easily be achieved through validations in Rails by writing the following line of code within the Model folder for a particular class:
Now, any user of our application attempting to add a wine into the application database without any of the aforementioned will be prevented from doing so. Additionally, the user will (automatically) be shown an error message describing why the instance was prevented from entering (i.e. being saved) into the database:
The above image shows a wine instance that has been prevented from entering our database since the user has not given a category to the wine — as described by the error message category can’t be blank. To further the example, trying to save an instance without any of the fields populated would produce the following:
Validating for the presence of certain attributes is important; however these are not the only validations we have at our disposal. Clearly, the year and rating attributes should contain some type of numerical value, and perhaps some type of restrictions on those numerical values. Should we allow a rating to extend pass a certain value? Should a year be allowed to be sometime in the distant future? Probably not. With validations we can ensure that a certain data-type is entered into the following fields — and also place additional restrictions on these values:
Notice the following errors given by attempting to save a wine with the following rating and year highlighted in the image below:
Clearly, there are many built-in validations we may use in order to ensure the integrity of the data that enters an application database. However, we also may create custom validations in order to tailor data for our application specific needs. Most wine enthusiasts are aware of the fact: champagne may only come from France. However, Rails is not a wine enthusiast and knows nothing about white wine, French wine — or any type of wine. Therefore, to account for the aforementioned we would have to create a custom validation. This is done by writing a method within our model — adding an error message as part of the method; and then using the method as a validation within our model. Creating a method that checks to see if the selected category is champagne as well as whether or not the selected country is France may appear as the following:
After writing the above method we would then place this method within a validation:
The two components in tandem with each other has now educated our program — which will no longer allow wine to persist into our database with a category of champagne unless the country of origin that is selected is France:
Notice how the method we created prevents the wine from being persisted into the database since the category selected is champagne and the country of origin is Argentina. Additionally, note how our custom error message — specified in our method appears at the top of the form.
Validations are clearly an integral part of web applications in Rails. Please feel free to view the code in my application with the Github link and YouTube description provided below — Cheers!