Presence Validations Ensuring That an Attribute Value Is Not Left Blank

In this demonstration, I will show how to add a presence validation to an existing model class. In general, model validations are used to ensure that only valid data are saved to the database. The pre-defined presence validation helper is used specifically to ensure that a specified model attribute cannot be left empty or blank.

General Steps

In general, the steps for adding a presence validation to a model class are as follows.

Validating the Presence of a Title in a Review Model Object

To demonstrate the steps for adding a presence validation to a model class, we will be building upon a movie-review base app by adding a validation to the existing Review model class (depicted in Figure 1) that ensures that a review’s title attribute is never left blank.

A class diagram depicting a Review model class with the following attributes: a title string, a score decimal number, body text, a genre string, a link string, a release date, and a review date

Figure 1. The Review model class.

Base App Code

Step 1 Add a Validation Declaration to the Model Class

To add a presence validation for the title attribute of the Review model class, we add a validates declaration to the class definition (found in app/models/review.rb), like this:

# == Schema Information
#
# Table name: reviews
#
#  id           :bigint           not null, primary key
#  body         :text
#  genre        :string
#  link         :string
#  release_date :date
#  review_date  :date
#  score        :decimal(, )
#  title        :string
#  created_at   :datetime         not null
#  updated_at   :datetime         not null
#
class Review < ApplicationRecord

  validates :title, presence: true

end

Breaking down this line of code, the call to validates adds the specified validation to the model class. The :title argument specifies that the validation will apply to the title attribute of the class. The presence: true argument specifies that the presence validation helper will be applied to the attribute. For a string attribute, like title, this validation ensures that the attribute is not nil, an empty string (""), or a string containing only whitespace characters.

Test It!

To confirm that we made this change correctly, we test it using the Rails console.

To start the console, we run this command:

rails console

As a first test of our presence model validation, we check whether we can create and save valid Review objects in the database by entering a call to the create class method, like this:

r = Review.create(
  title: 'The Matrix',
  score: 9.7,
  body: 'I love the Matrix so much that I saw it eight times in theaters and had plastic surgery to look like Keanu Reeves. Now everyone asks if I\'m Keanu. My life is actually kind of sad.',
  genre: 'Science Fiction',
  link: 'https://www.imdb.com/title/tt0133093/',
  release_date: Date.new(1999, 3, 31),
  review_date: Date.new(2020, 12, 2)
)

The console output should look like this:

  TRANSACTION (0.3ms)  BEGIN
  Review Create (1.8ms)  INSERT INTO "reviews" ("body", "genre", "link", "release_date", "review_date", "score", "title", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id"  [["body", "I love the Matrix so much that I saw it eight times in theaters and had plastic surgery to look like Keanu Reeves. Now everyone asks if I'm Keanu. My life is actually kind of sad."], ["genre", "Science Fiction"], ["link", "https://www.imdb.com/title/tt0133093/"], ["release_date", "1999-03-31"], ["review_date", "2020-12-02"], ["score", "9.7"], ["title", "The Matrix"], ["created_at", "2021-01-29 22:01:23.998414"], ["updated_at", "2021-01-29 22:01:23.998414"]]
  TRANSACTION (0.3ms)  COMMIT
 => #<Review id: 1, body: "I love the Matrix so much that I saw it eight time...", genre: "Science Fiction", link: "https:... 

Note that the call to create succeeded, returning a Review object.

Also, note that we stored a reference to the returned Review object in a variable, r.

Next, we test that our presence validation will detect a title value of nil.

We first set the title to nil, like this:

r.title = nil

We then attempt to save this change to the database, like this:

r.save

The output of the command should look only like this:

 => false

The return value of false indicates that the call to save was unsuccessful. Note that no SQL commands are printed, because the change wasn’t saved to the database.

When commands that save model objects to the database fail, error messages are attached to the model object. To inspect the error messages in this case, we use the errors method, like this:

r.errors.full_messages

The call should return this value:

 => ["Title can't be blank"] 

Note that the error message indicates that it was the blank title that caused the save method to fail.

If we wanted to, we could repeat the above steps, only setting the title attribute to an empty string ("") or a string with only whitespace characters (e.g., " "); however, these test should be unnecessary—if the validation works for nil values, it should also work for these blank string values.

Satisfied that our presence validation is functioning as expected, we quit the Rails console by entering this command:

exit

Step 1 Changeset

Step 2 Add a Model Test for the Validation

To add a model test for verifying that our presence validation works correctly, we follow the general steps from the model tests demo.

Substep Create fixtures. In the base app, the Review model class already has this test fixture (in test/fixtures/reviews.yml):

# == Schema Information
#
# Table name: reviews
#
#  id           :bigint           not null, primary key
#  body         :text
#  genre        :string
#  link         :string
#  release_date :date
#  review_date  :date
#  score        :decimal(, )
#  title        :string
#  created_at   :datetime         not null
#  updated_at   :datetime         not null
#

one:
  title: The Matrix
  score: 9.7
  body: I love the Matrix so much that I saw it eight times in theaters and had plastic surgery to look like Keanu Reeves. Now everyone asks if I'm Keanu. My life is actually kind of sad.
  genre: Science Fiction
  link: https://www.imdb.com/title/tt0133093/
  release_date: 1999-03-31
  review_date: 2020-12-02

This fixture will be sufficient for this model test, so we can skip this substep.

Substep Add an empty model test. We add an empty model test to the ReviewTest class (in test/models/review_test.rb), like this:

# == Schema Information
#
# Table name: reviews
#
#  id           :bigint           not null, primary key
#  body         :text
#  genre        :string
#  link         :string
#  release_date :date
#  review_date  :date
#  score        :decimal(, )
#  title        :string
#  created_at   :datetime         not null
#  updated_at   :datetime         not null
#
require "test_helper"

class ReviewTest < ActiveSupport::TestCase

  test "all fixtures should be valid" do
    review_one = reviews(:one)
    assert review_one.valid?, review_one.errors.full_messages.inspect
  end

  test "title must be present" do
    
  end

end

Note that the ReviewTest class contained one test already ("all fixtures should be valid") to verify that all the fixtures are valid, and we added our new test ("title must be present") beneath it.

Substep Retrieve the fixture object. To retrieve the fixture object, we add a call to the fixture-retrieving method, reviews, like this:

class ReviewTest < ActiveSupport::TestCase

  …

  test "title must be present" do
    review_one = reviews(:one)
  end

end

Substep Manipulate the fixture object. Because we want to ensure that a review with a blank title attribute cannot be saved to the database, we set the title of the retrieved fixture object to '', like this:

class ReviewTest < ActiveSupport::TestCase

  …

  test "title must be present" do
    review_one = reviews(:one)
    review_one.title = ''
  end

end

Substep Check for expected behavior. In this case, the test succeeds if the validation catches the error, so we want to make sure the fixture is not valid. Thus, we use the assert_not function in conjunction with the valid? method, like this:

class ReviewTest < ActiveSupport::TestCase

  …

  test "title must be present" do
    review_one = reviews(:one)
    review_one.title = ''
    assert_not review_one.valid?
  end

end

Test It!

To confirm that we made this change correctly, we run the test we created, like this:

rails test -v

Technically, this command runs all the tests written for the web app. It should produce output similar to this:

Running via Spring preloader in process 78139
Run options: -v --seed 38274

# Running:

ReviewTest#test_all_fixtures_should_be_valid = 0.06 s = .
ReviewTest#test_title_must_be_present = 0.06 s = .

Finished in 0.204849s, 9.7633 runs/s, 9.7633 assertions/s.
2 runs, 2 assertions, 0 failures, 0 errors, 0 skips

Note that the output shows that our newly created test, ReviewTest#test_title_must_be_present, ran and that there were “0 failures” and “0 errors”. Also, the output shows “2 runs”, because the ReviewTest class contains two tests, and it shows “2 assertions”, because each of those tests executed a call to one assertion.

Step 2 Changeset

Conclusion

Following the above steps, we have now added a presence validation to a model class to ensure that one of its attributes never has a blank value saved to the database.

Demo App Code