Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Ruby on Rails - Validations in Rails

Introduction

Validations are a crucial part of any web application to ensure that only valid data is saved into the database. Rails provides a variety of built-in validation helpers that make it easy to check the validity of data before saving it to the database. This guide will cover the basics of implementing validations in Rails models.

Key Points:

  • Validations help ensure that only valid data is saved into the database.
  • Rails provides built-in validation helpers for common validation needs.
  • This guide covers how to use these validation helpers and how to create custom validations.

Built-in Validation Helpers

Rails provides several built-in validation helpers to handle common validation needs:

  • presence: Ensures that a specified attribute is not empty.
  • length: Ensures that a specified attribute has a valid length.
  • uniqueness: Ensures that a specified attribute is unique within the database.
  • format: Ensures that a specified attribute matches a given regular expression.
  • numericality: Ensures that a specified attribute is a number.

Using presence Validation

The presence validation ensures that the specified attribute is not empty. Here is an example:

# app/models/article.rb
class Article < ApplicationRecord
  validates :title, presence: true
  validates :body, presence: true
end
                

Using length Validation

The length validation ensures that the specified attribute has a valid length. Here is an example:

# app/models/article.rb
class Article < ApplicationRecord
  validates :title, length: { minimum: 5, maximum: 100 }
  validates :body, length: { minimum: 10 }
end
                

Using uniqueness Validation

The uniqueness validation ensures that the specified attribute is unique within the database. Here is an example:

# app/models/user.rb
class User < ApplicationRecord
  validates :email, uniqueness: true
end
                

Using format Validation

The format validation ensures that the specified attribute matches a given regular expression. Here is an example:

# app/models/user.rb
class User < ApplicationRecord
  validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }
end
                

Using numericality Validation

The numericality validation ensures that the specified attribute is a number. Here is an example:

# app/models/product.rb
class Product < ApplicationRecord
  validates :price, numericality: { greater_than: 0 }
end
                

Custom Validations

In addition to the built-in validations, you can also create custom validations by defining methods in your model. Here is an example:

# app/models/article.rb
class Article < ApplicationRecord
  validate :title_is_not_clickbait

  private

  def title_is_not_clickbait
    if title.present? && title.match?(/Won't Believe|Secret|Top \d|Guess/)
      errors.add(:title, "must be clickbait-free")
    end
  end
end
                

Conditional Validations

Sometimes, you may want to apply validations only under certain conditions. You can achieve this using the :if and :unless options. Here is an example:

# app/models/user.rb
class User < ApplicationRecord
  validates :email, presence: true, if: :email_required?

  private

  def email_required?
    !admin?
  end
end
                

Conclusion

Validations are an essential part of ensuring data integrity in your Rails applications. By using built-in validation helpers, custom validations, and conditional validations, you can effectively manage the quality of the data stored in your database.