Author: Mary Frann

Details of the belongs_to relationship in Ruby

A belongs_to relationship creates a one-to-one correspondence with another model. In database terms, this relationship says that this class contains a foreign key. If the foreign key contains another class, has_one should be used instead. Methods added by belongs_to When you declare a belongs_to relationship, the declaring class automatically receives five methods related to the […]

Rails Active Record Connection

has_many connection A has_many relationship indicates a one-to-many connection to another model. This relationship is often on the “other side” of the belongs_to relationship. This relationship indicates that each model instance has zero or more instances of another model. For example, in an application containing authors and books, the author model might be declared like […]

Transactional callbacks in Ruby on Rails

There are two additional callbacks that are fired when a database transaction completes: after_commit and after_rollback. These callbacks are very similar to the after_save callback, except that they are not fired until the database changes have been committed or reversed. They are most useful when your Active Record models need to interact with external systems […]

Callbacks in Rails

WARNING. after_save is fired on both creation and update, but always after the more specific after_create and after_update callbacks, regardless of the order in which the macro calls are fired. NOTE: The before_destroy callback must be placed before dependent: :destroy bindings (or use the prepend: true option) to ensure that they are executed before the […]

Rails built-in validators

All Rails built-in validators populate the details hash with the appropriate validator type. errors[:base] You can add error messages that refer to the state of the object as a whole rather than to a single attribute. This method can be used if you want to say that an object is invalid, regardless of the values […]

General validation options in Ruby

There are several common validation options: :allow_nil The :allow_nil option skips validation when the value being checked is nil. class Coffee < ApplicationRecord   validates :size, inclusion: { in: %w(small medium large),     message: “%{value} is not a valid size” }, allow_nil: true end See the message documentation for all message argument options. :allow_blank The […]

Helpers in Ruby on Rails

numericality This helper validates that your attributes only have numeric values. By default, this will match the possible sign of the first character, followed by an integer or floating point number. To specify that only integer values ​​are allowed, set :only_integer to true. If you set :only_integer to true then the regular expression will be […]

Validation errors in Ruby on Rails

We’ll look at validation errors in more detail in the Dealing with Validation Errors section. errors.details To check which validation has failed on an invalid attribute, errors.details[:attribute] can be used. It returns an array of hashes with the key :error to get the validator symbol: classPerson < ApplicationRecord   validates :name, presence: true end >>person […]

RoR Active Record Validations

This tutorial will teach you how to validate the state of objects before they are sent to the database using Active Record’s validation feature. After reading the guide, you will know: How to use the built-in Active Record validation helpers How to create your own validation methods How to deal with error messages generated during […]