Attribute validation

ActiveRecord validations can be used to validate model attributes, before creating an instance of the model. Validations prevent bad data from entering the database. Let’s say you have a person model, with a height attribute. The height attribute is specified in inches, and must be a decimal number. Using an ActiveRecord validation, you could validate the height attribute.

class Person < ApplicationRecord
  validates: :height, presence: true, numericality: { only_float: true, greater_than: 0 }

You could write a complementary test to capture this validation.

# test/fixtures/users.yml
    name: Joe Schmoe
    height: 173

# test/models/user_test.rb
class UserTest < ActiveSupport::TestCase
  def setup
    @user = users(:one)

  test 'User is valid with valid attributes' do
    assert @user.valid?

  test 'User is invalid with invalid height' do
    @user.height = 'string'
    refute @user.valid?, 'Saved user without a non-numeric height'