<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Eapen Zacharias</title>
    <description>The latest articles on Forem by Eapen Zacharias (@eapenzac).</description>
    <link>https://forem.com/eapenzac</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F887705%2Fd2951bda-be7a-4951-9260-d828322622c1.png</url>
      <title>Forem: Eapen Zacharias</title>
      <link>https://forem.com/eapenzac</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/eapenzac"/>
    <language>en</language>
    <item>
      <title>Introduction to Ruby on Rails: Getting Started with Web Development</title>
      <dc:creator>Eapen Zacharias</dc:creator>
      <pubDate>Fri, 04 Aug 2023 17:34:52 +0000</pubDate>
      <link>https://forem.com/eapenzac/introduction-to-ruby-on-rails-getting-started-with-web-development-39g8</link>
      <guid>https://forem.com/eapenzac/introduction-to-ruby-on-rails-getting-started-with-web-development-39g8</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction to Ruby on Rails: Getting Started with Web Development&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ruby on Rails, often simply referred to as Rails, is a powerful and popular open-source web application framework written in the Ruby programming language. It provides developers with a set of tools, conventions, and best practices to streamline the process of building robust and scalable web applications. In this tutorial, we'll dive into the basics of Ruby on Rails and explore key concepts that will help you embark on your web development journey.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Table of Contents:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What is Ruby on Rails?&lt;/li&gt;
&lt;li&gt;Understanding MVC Architecture&lt;/li&gt;
&lt;li&gt;Why Choose Ruby on Rails?&lt;/li&gt;
&lt;li&gt;When to Use Ruby on Rails&lt;/li&gt;
&lt;li&gt;Prerequisites&lt;/li&gt;
&lt;li&gt;Setting Up Your Development Environment&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;What is Ruby on Rails?&lt;/strong&gt;&lt;br&gt;
Ruby on Rails is a framework designed to simplify and accelerate web application development. It follows the Model-View-Controller (MVC) architectural pattern, which divides the application's components into three main interconnected parts: Models, Views, and Controllers. This separation allows developers to manage different aspects of an application separately, promoting code organization, reusability, and maintainability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding MVC Architecture&lt;/strong&gt;&lt;br&gt;
MVC, or Model-View-Controller, is a fundamental concept in Rails. Here's a brief overview of each component:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Model&lt;/strong&gt;: Represents the application's data and business logic. It interacts with the database and encapsulates the rules governing data manipulation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;View&lt;/strong&gt;: Handles the presentation layer and displays data to users. It generates the user interface and often includes HTML templates.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Controller&lt;/strong&gt;: Acts as an intermediary between the Model and View. It processes user requests, interacts with the Model to retrieve or modify data, and renders the appropriate View.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why Choose Ruby on Rails?&lt;/strong&gt;&lt;br&gt;
Ruby on Rails offers several advantages that make it a preferred choice for web development:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Convention over Configuration&lt;/strong&gt;: Rails follows sensible conventions, reducing the need for extensive configuration. This speeds up development and encourages consistency.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DRY Principle (Don't Repeat Yourself)&lt;/strong&gt;: Rails promotes code reuse and modular design, minimizing redundancy and making maintenance easier.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Gems and Plugins&lt;/strong&gt;: Rails provides a rich ecosystem of Gems (libraries) and Plugins that extend its functionality, allowing developers to add features without starting from scratch.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rapid Development&lt;/strong&gt;: With built-in tools for common tasks, such as database migrations and scaffolding, Rails accelerates the development process.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When to Use Ruby on Rails&lt;/strong&gt;&lt;br&gt;
Ruby on Rails is an excellent choice for various web application scenarios:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prototyping&lt;/strong&gt;: Quickly create prototypes to test ideas and concepts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Startups&lt;/strong&gt;: Build an MVP (Minimum Viable Product) rapidly and iterate based on user feedback.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Small to Medium-sized Projects&lt;/strong&gt;: Rails is well-suited for projects with predefined requirements and budgets.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Developer Productivity&lt;/strong&gt;: Developers can focus on application logic rather than low-level configurations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;br&gt;
Before diving into Rails development, ensure you have a basic understanding of programming concepts, HTML, and CSS. Familiarity with the Ruby programming language is helpful but not mandatory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting Up Your Development Environment&lt;/strong&gt;&lt;br&gt;
To begin coding with Ruby on Rails, you'll need to set up your development environment. This involves installing Ruby, Rails, and other necessary tools. The exact steps depend on your operating system. In the next tutorial, we'll guide you through the installation process for various platforms.&lt;/p&gt;

&lt;p&gt;In this tutorial, we introduced you to Ruby on Rails, explored its MVC architecture, highlighted its advantages, and discussed scenarios where it's an optimal choice. As you continue through this series, you'll dive deeper into each aspect of Rails development, learning how to build full-fledged web applications from scratch.&lt;/p&gt;

&lt;p&gt;Stay tuned for the next tutorial, where we'll walk you through the process of setting up your development environment and getting your first Ruby on Rails application up and running. Happy coding!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>rails</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Ruby on Rails ActiveRecord Associations</title>
      <dc:creator>Eapen Zacharias</dc:creator>
      <pubDate>Tue, 12 Jul 2022 18:31:20 +0000</pubDate>
      <link>https://forem.com/eapenzac/ruby-on-rails-activerecord-associations-30am</link>
      <guid>https://forem.com/eapenzac/ruby-on-rails-activerecord-associations-30am</guid>
      <description>&lt;p&gt;Much of the power of Ruby on Rails lies in creating good associations to keep our data well organized, so that it can be used efficiently.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdn2ge1ohj5nr2xqi3h4j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdn2ge1ohj5nr2xqi3h4j.png" alt="Association Symbols"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are three main association types.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;One-to-One Association&lt;/strong&gt;: One object is related with one and only object.&lt;br&gt;
Example: If there are two entities &lt;code&gt;Person(Id, Name, Age, Address)&lt;/code&gt; and &lt;code&gt;Passport(Passport_id, Passport_no)&lt;/code&gt;. So, each person can have only one passport and each passport belongs to only one person.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjjjhyynptjr18z88q3t3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjjjhyynptjr18z88q3t3.png" alt="One-to-One Association"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;One-to-Many Association:&lt;/strong&gt; One object has many objects and many objects belongs to the One object. &lt;br&gt;
Example: If there are two entity type &lt;code&gt;Customer&lt;/code&gt; and &lt;code&gt;Account&lt;/code&gt; then each &lt;code&gt;Customer&lt;/code&gt; can have more than one &lt;code&gt;Account&lt;/code&gt; but each &lt;code&gt;Account&lt;/code&gt; is held by only one &lt;code&gt;Customer&lt;/code&gt;. In this example, we can say that each Customer is associated with many Account. So, it is a one-to-many relationship. But, if we see it the other way i.e many Account is associated with one Customer then we can say that it is a many-to-one relationship.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyam6r4ese89769992zed.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyam6r4ese89769992zed.png" alt="One-to-Many Association"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Many-to-Many Association:&lt;/strong&gt; Each object of the first class can be related to one or more than one object of the second class and a single object of the second class can be related to one or more than one object of the first class.&lt;br&gt;
NOTE: For using Many-to-Many association we need a &lt;strong&gt;joint table&lt;/strong&gt; as we can’t include many foreign keys to a table. A joint table serves as a way to make links between two different tables. The joint table will have foreign keys of each pair of related objects from the both tables.&lt;br&gt;
Example: If there are two entity type &lt;code&gt;Customer&lt;/code&gt; and &lt;code&gt;Product&lt;/code&gt; then each customer can buy more than one product and a product can be bought by many different customers.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fovrdi6iki1aqeishiyqg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fovrdi6iki1aqeishiyqg.png" alt="Many-to-Many Association"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Association Method
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;One-to-One relationships&lt;/strong&gt;: &lt;code&gt;has_one, belongs_to&lt;/code&gt;, &lt;code&gt;has_one :through&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One-to-Many relationships&lt;/strong&gt;: &lt;code&gt;has_many, belongs_to&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Many-to-Many relationships&lt;/strong&gt;: &lt;code&gt;has_and_belongs_to_many&lt;/code&gt;, &lt;code&gt;has_many :through&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;code&gt;belongs_to&lt;/code&gt;&lt;/strong&gt; Association
&lt;/h3&gt;

&lt;p&gt;A belongs_to association sets up a connection with another model, and declares model "belongs to" one instance of the other model.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; When you define belongs_to inside a model, the other object that that belongs_to is not optional. If it belongs to it, then that other object has to be assigned. This happens because Rails adds a validation that check if the referenced object is present. Please also note that order versions of Rails do not do that.&lt;br&gt;
You can disable this validation by passing &lt;code&gt;{:optional =&amp;gt; true}&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;For example, if your application includes &lt;code&gt;person&lt;/code&gt; and &lt;code&gt;passport&lt;/code&gt;, and each &lt;code&gt;passport&lt;/code&gt; can be assigned to exactly one &lt;code&gt;person&lt;/code&gt;, you'd declare the &lt;code&gt;passport&lt;/code&gt; model this way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Passport&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:person&lt;/span&gt; &lt;span class="c1"&gt;# this validates the presence of the person object&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Passport&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:person&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;optional: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt; &lt;span class="c1"&gt;# does not validates the presence of the person object&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;code&gt;has_one&lt;/code&gt;&lt;/strong&gt; Association
&lt;/h3&gt;

&lt;p&gt;A &lt;code&gt;has_one&lt;/code&gt; association shows that another model is referenced to this model. That model can be fetched through this association.&lt;/p&gt;

&lt;p&gt;For example, if each &lt;code&gt;person&lt;/code&gt; in your application has only one &lt;code&gt;passport&lt;/code&gt;, you'd declare the &lt;code&gt;person&lt;/code&gt; model like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;has_one&lt;/span&gt; &lt;span class="ss"&gt;:passport&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;code&gt;has_many&lt;/code&gt;&lt;/strong&gt; Association
&lt;/h3&gt;

&lt;p&gt;A &lt;code&gt;has_many&lt;/code&gt;association indicates a one-to-many connection with another model and often used with &lt;code&gt;belongs_to&lt;/code&gt; on the other model. This association declares that each instance of the model has zero or more instances of another model.&lt;/p&gt;

&lt;p&gt;For example, in an application containing &lt;code&gt;customers&lt;/code&gt; and &lt;code&gt;accounts&lt;/code&gt;, the &lt;code&gt;customers&lt;/code&gt; model could be declared like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Customer&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:accounts&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;code&gt;has_many :through&lt;/code&gt;&lt;/strong&gt; Association
&lt;/h3&gt;

&lt;p&gt;A &lt;code&gt;has_many :through&lt;/code&gt; association is used to set up a many-to-many connection with another model. This declares a model can be matched with zero or more instances of another model through a third model.&lt;br&gt;
For example, consider a online shop where customers make purchases to get products. The association declarations could look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Customer&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:purchases&lt;/span&gt;
  &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:products&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;through: :purchases&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Purchase&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:customer&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:product&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Product&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:purchases&lt;/span&gt;
  &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:customers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;through: :purchases&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;code&gt;has_one :through&lt;/code&gt;&lt;/strong&gt; Association
&lt;/h3&gt;

&lt;p&gt;A &lt;code&gt;has_one :through&lt;/code&gt; association sets up a one-to-one connection with another model through a third model. This declares the model can be matched with one instance of another model through a third model.&lt;/p&gt;

&lt;p&gt;For example, if each person has one passport, and each passport is associated with one passport_issue, then the models could look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;has_one&lt;/span&gt; &lt;span class="ss"&gt;:passport&lt;/span&gt;
  &lt;span class="n"&gt;has_one&lt;/span&gt; &lt;span class="ss"&gt;:passport_issue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;through: :passport&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Passport&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:person&lt;/span&gt;
  &lt;span class="n"&gt;has_one&lt;/span&gt; &lt;span class="ss"&gt;:passport_issue&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PassportIssue&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:passport&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;code&gt;has_and_belongs_to_many&lt;/code&gt;&lt;/strong&gt; Association
&lt;/h3&gt;

&lt;p&gt;A &lt;code&gt;has_and_belongs_to_many&lt;/code&gt; association creates a direct many-to-many connection with another model, without the help of a third model and declares this model can be matched with zero or more instances of another model&lt;/p&gt;

&lt;p&gt;For example, if your application includes assemblies and parts, with each assembly having many parts and each part appearing in many assemblies, you could declare the models this way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Assembly&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;has_and_belongs_to_many&lt;/span&gt; &lt;span class="ss"&gt;:parts&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Part&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;has_and_belongs_to_many&lt;/span&gt; &lt;span class="ss"&gt;:assemblies&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Choosing Between &lt;code&gt;has_many :through&lt;/code&gt; and &lt;code&gt;has_and_belongs_to_many&lt;/code&gt;:&lt;/strong&gt; The simplest rule of thumb is that you should set up a &lt;code&gt;has_many :through&lt;/code&gt; relationship if you need to work with the relationship model as an independent entity. If you don't need to do anything with the relationship model, it may be simpler to set up a &lt;code&gt;has_and_belongs_to_many&lt;/code&gt; relationship (though you'll need to remember to create the joining table in the database).&lt;/p&gt;

&lt;h2&gt;
  
  
  Polymorphic Associations
&lt;/h2&gt;

&lt;p&gt;A polymorphic association allows a model to belong to multiple other models. In this case, we might create a Profile model and a Blog model, and both have many references to Images.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Image&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:imageable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;polymorphic: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Profile&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:images&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;as: :imageable&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Blog&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:images&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;as: :imageable&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can consider polymorphic &lt;code&gt;belongs_to&lt;/code&gt; declares that any models can set up an interface with model and use it. In this case we can retrieve an image from a profile instance using &lt;code&gt;@profile.image&lt;/code&gt; and a collection of images from a blog instance using &lt;code&gt;@blog.images&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Features in ActiveRecords
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;:dependent =&amp;gt; :destroy&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;When the object is destroyed, destroy will be called on its associated objects.&lt;/p&gt;

&lt;p&gt;Example: In this case when a user is deleted all the comments created by the user also gets destroyed&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
    &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:comments&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;dependent: :destroy&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Comment&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
    &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:user&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;:dependent =&amp;gt; :delete_all&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The difference here is that :delete_all, like the delete_all method, is skipping the callbacks that are in that comment object. It just goes straight to the database, and writes SQL to delete them. Destroy is a safer choice, because comment may have callbacks as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
    &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:comments&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;dependent: :delete_all&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Comment&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
    &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:user&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Joining Tables
&lt;/h2&gt;

&lt;p&gt;Once we have associations between our models, we can start to work with them in interesting ways. And one of those ways is by joining tables during our queries. If you're familiar with working with databases, you'll know that there are times when we want to be able to join two different tables together during a query. So we can query columns on more than one table.&lt;/p&gt;

&lt;p&gt;We can use SQL joins to query columns on several tables.&lt;/p&gt;

&lt;p&gt;Example: The given code will return Products for which its category is active&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;joins&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:category&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'categories.active = 1'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# or you can use a hash format&lt;/span&gt;
&lt;span class="no"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;joins&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:category&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;categories: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;active: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Here are the few ways we can join a table:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Using a String SQL Fragment:&lt;/strong&gt; We can provide the raw SQL specifying the JOIN clause
&lt;code&gt;Author.joins("INNER JOIN books ON books.author_id = authors.id AND books.out_of_print = FALSE")&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Joining a Single Association:&lt;/strong&gt;
&lt;code&gt;Product.joins(:category)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Joining Multiple Associations:&lt;/strong&gt;
&lt;code&gt;Product.joins(:category, :inventory)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By joining tables together with the joins method, you have the ability to start working with your associations in interesting ways.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Thanks For Reading, Follow Me For More&lt;br&gt;
Share your suggestions, doubts and feedback in the comments section!&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>associations</category>
      <category>database</category>
    </item>
    <item>
      <title>Ruby on Rails ActiveRecord Callbacks</title>
      <dc:creator>Eapen Zacharias</dc:creator>
      <pubDate>Tue, 05 Jul 2022 23:54:48 +0000</pubDate>
      <link>https://forem.com/eapenzac/ruby-on-rails-activerecord-callbacks-597a</link>
      <guid>https://forem.com/eapenzac/ruby-on-rails-activerecord-callbacks-597a</guid>
      <description>&lt;p&gt;Callbacks are a way that ActiveRecord gives us to intervene in an object's life cycle.&lt;br&gt;
It's almost like it pauses before or after key moments in its internal processes, to check in with you to see if you have code customization that you'd like to run before it continues. It allows you then to run code automatically before or after an Active Record object is created, saved, updated, deleted, validated, or loaded from the database.&lt;/p&gt;

&lt;p&gt;We could use this callbacks to perform various tasks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set values for the current record&lt;/li&gt;
&lt;li&gt;Create, update or destroy other records&lt;/li&gt;
&lt;li&gt;Perform cleanup and housekeeping tasks&lt;/li&gt;
&lt;li&gt;Log information&lt;/li&gt;
&lt;li&gt;Send an email or make an API call&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The order of callbacks is &lt;code&gt;before&lt;/code&gt;, &lt;code&gt;around&lt;/code&gt;, &lt;code&gt;after&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;before&lt;/code&gt;&lt;/strong&gt;: happens before the action happens&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;around&lt;/code&gt;&lt;/strong&gt;: can have logic before and after the action being run. The code before yield is invoked before the action and the code after is called after the action&lt;/p&gt;

&lt;p&gt;example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;around_create&lt;/span&gt; &lt;span class="ss"&gt;:method_name&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;method_name&lt;/span&gt;
  &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Before action code'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;yield&lt;/span&gt;
  &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'After action this will run'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;after&lt;/code&gt;&lt;/strong&gt; : runs after an action is complete &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here is a list of Rails inbuilt Active Record Callback methods:&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating an Object
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;before_validation&lt;/code&gt;  Defines a callback that will get called right before validation.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;after_validation&lt;/code&gt;  Defines a callback that will get called right after validation.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;before_save&lt;/code&gt;  Registers a callback to be called before a record is saved.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;around_save&lt;/code&gt;  Registers a callback to be called around the save of a record.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;before_create&lt;/code&gt;  Registers a callback to be called before a record is created.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;around_create&lt;/code&gt;  Registers a callback to be called around the creation of a record.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;after_create&lt;/code&gt;  Registers a callback to be called after a record is created.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;after_save&lt;/code&gt;  Registers a callback to be called after a record is saved.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;after_commit&lt;/code&gt;  This callback is called after a record has been created, updated, or destroyed. Example: &lt;code&gt;after_commit :do_baz, on: :destroy&lt;/code&gt;  &lt;code&gt;after_commit :do_bar_baz, on: [:update, :destroy]&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;after_create_commit&lt;/code&gt;: Shortcut for &lt;code&gt;after_commit :hook, on: :create&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;after_destroy_commit&lt;/code&gt;: Shortcut for &lt;code&gt;after_commit :hook, on: :destroy&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;after_save_commit&lt;/code&gt;:  Shortcut for &lt;code&gt;after_commit :hook, on: [ :create, :update ]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;after_update_commit&lt;/code&gt;: Shortcut for &lt;code&gt;after_commit :hook, on: :update&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;after_rollback&lt;/code&gt;  This callback is called after a create, update, or destroy are rolled back. Note that all options of &lt;code&gt;after_commit&lt;/code&gt; works for &lt;code&gt;after_rollback&lt;/code&gt; too.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Updating an Object
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;before_validation&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;after_validation&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;before_save&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;around_save&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;before_update&lt;/code&gt;  Registers a callback to be called before a record is updated.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;around_update&lt;/code&gt;  Registers a callback to be called around the update of a record.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;after_update&lt;/code&gt;  Registers a callback to be called after a record is updated.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;after_save&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;after_commit&lt;/code&gt; / &lt;code&gt;after_rollback&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Destroying an Object
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;before_destroy&lt;/code&gt;  Registers a callback to be called before a record is destroyed.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;around_destroy&lt;/code&gt;  Registers a callback to be called around the destruction of a record.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;after_destroy&lt;/code&gt;  Registers a callback to be called after a record is destroyed.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;after_commit&lt;/code&gt; / &lt;code&gt;after_rollback&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Use Callbacks to Automate Actions
&lt;/h2&gt;

&lt;p&gt;In order to use the available callbacks, you need to register them. You can implement the callbacks as ordinary methods and use a macro-style class method to register them as callbacks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;validates&lt;/span&gt; &lt;span class="ss"&gt;:login&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;presence: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;

  &lt;span class="n"&gt;before_validation&lt;/span&gt; &lt;span class="ss"&gt;:ensure_login_has_a_value&lt;/span&gt;

  &lt;span class="kp"&gt;private&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;ensure_login_has_a_value&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;login&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;nil?&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;login&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;blank?&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The macro-style class methods can also receive a block. Consider using this style if the code inside your block is so short that it fits in a single line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;validates&lt;/span&gt; &lt;span class="ss"&gt;:login&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;presence: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;

  &lt;span class="n"&gt;before_create&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;login&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;capitalize&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;blank?&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Callbacks can also be registered to only fire on certain life cycle events:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;before_validation&lt;/span&gt; &lt;span class="ss"&gt;:normalize_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;on: :create&lt;/span&gt;

  &lt;span class="c1"&gt;# :on takes an array as well&lt;/span&gt;
  &lt;span class="n"&gt;after_validation&lt;/span&gt; &lt;span class="ss"&gt;:set_location&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;on: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="ss"&gt;:create&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:update&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;

  &lt;span class="kp"&gt;private&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;normalize_name&lt;/span&gt;
      &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;downcase&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;titleize&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;set_location&lt;/span&gt;
      &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;location&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;LocationService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conditional Callbacks
&lt;/h2&gt;

&lt;p&gt;By default callbacks will run all the time at the point at which you've registered them to execute. But we can also execute callbacks conditionally. You can tell your model to execute callback methods only when a particular condition is met.&lt;/p&gt;

&lt;p&gt;We can do this by using the following options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;:if&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;:unless&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here are few examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;validates&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;presence: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;if: :admin?&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;admin?&lt;/span&gt;
    &lt;span class="n"&gt;conditional&lt;/span&gt; &lt;span class="n"&gt;here&lt;/span&gt; &lt;span class="n"&gt;that&lt;/span&gt; &lt;span class="n"&gt;returns&lt;/span&gt; &lt;span class="n"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# For negative conditional you can use unless&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;validates&lt;/span&gt; &lt;span class="ss"&gt;:first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;presence: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;unless: &lt;/span&gt;&lt;span class="no"&gt;Proc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;present?&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# You can also pass a string, which will be executed via instance_eval&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;validates&lt;/span&gt; &lt;span class="ss"&gt;:first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;presence: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;if: &lt;/span&gt;&lt;span class="s1"&gt;'last_name.blank?'&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Skipping Callbacks
&lt;/h2&gt;

&lt;p&gt;Every now and then it's useful to be able to skip callbacks specially when we just have to update . &lt;/p&gt;

&lt;p&gt;The first way is that if you have an instance of an object and you want to update it, you can call &lt;code&gt;update_column&lt;/code&gt; or &lt;code&gt;update_columns&lt;/code&gt;, or if you want to get rid of an object without having any callbacks, you can call &lt;code&gt;delete&lt;/code&gt;. Normally the method we use is destroy. Destroy does activate our callbacks, but delete does not. The reason these three methods skip our callbacks is because all three of them, construct SQL to submit directly to the database.&lt;br&gt;
&lt;strong&gt;Usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update_column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update_columns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;delete&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The other way we skip callback is if we have an ActiveRecord relation. We can either call &lt;strong&gt;update_all&lt;/strong&gt; or &lt;strong&gt;delete_all&lt;/strong&gt; on those relations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;relation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update_all&lt;/span&gt;
&lt;span class="c1"&gt;# example:&lt;/span&gt;
&lt;span class="no"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"modified_at &amp;lt; ?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;years&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ago&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;update_all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;active: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;relation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;delete_all&lt;/span&gt;
&lt;span class="c1"&gt;# example:&lt;/span&gt;
&lt;span class="no"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"created_at &amp;lt; ?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;years&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ago&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;delete_all&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; You can also use conditional callbacks to skip a callback for part of the business logic of your application.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Thanks For Reading, Follow Me For More&lt;br&gt;
Share your suggestions, doubts and feedback in the comments section!&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>rails</category>
      <category>activerecord</category>
      <category>ruby</category>
      <category>callbacks</category>
    </item>
    <item>
      <title>Ruby on Rails ActiveRecord Validations</title>
      <dc:creator>Eapen Zacharias</dc:creator>
      <pubDate>Tue, 05 Jul 2022 20:29:29 +0000</pubDate>
      <link>https://forem.com/eapenzac/rails-activerecord-validations-3638</link>
      <guid>https://forem.com/eapenzac/rails-activerecord-validations-3638</guid>
      <description>&lt;p&gt;Validations allow us to make sure that data meets a set of requirements before it's saved to the database. You can think of them as rules that our data must follow. It is always a good practice to check the validations from the models to make our models smarter by telling them about what should be considered valid data in the database and also regardless of where we're submitting data from the application, it'll still follow the rules. &lt;/p&gt;

&lt;h2&gt;
  
  
  Built in Validation Methods
&lt;/h2&gt;

&lt;p&gt;Rails provided many inbuilt validation helpers which covers most use cases when you are building an app. Here are the most important ones:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;# validates_presence_of&lt;/strong&gt; validates the attribute is not blank&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;validates_presence_of&lt;/span&gt; &lt;span class="ss"&gt;:atrr_name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;# Validates_length_of&lt;/strong&gt; check length requirements of the attribute&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;validates_length_of&lt;/span&gt; &lt;span class="ss"&gt;:attr_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:within&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;
&lt;span class="n"&gt;validates_length_of&lt;/span&gt; &lt;span class="ss"&gt;:attr_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:is&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;
&lt;span class="n"&gt;validates_length_of&lt;/span&gt; &lt;span class="ss"&gt;:attr_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:minimum&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:maximum&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;:minimum&lt;/code&gt; - The attribute cannot have less than the specified length.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:maximum&lt;/code&gt; - The attribute cannot have more than the specified length.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:in&lt;/code&gt; (or &lt;code&gt;:within&lt;/code&gt;) - The attribute length must be included in a given interval. The value for this option must be a range.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:is&lt;/code&gt; - The attribute length must be equal to the given value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;# Validates_numecality_of&lt;/strong&gt; check if the attribute is a number or not&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;validates_numericality_of&lt;/span&gt; &lt;span class="ss"&gt;:height&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:greater_than&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;3.0&lt;/span&gt;
&lt;span class="n"&gt;validates_numericality_of&lt;/span&gt; &lt;span class="ss"&gt;:height&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:less_than&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
&lt;span class="n"&gt;validates_numericality_of&lt;/span&gt; &lt;span class="ss"&gt;:height&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:equal_to&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;3.0&lt;/span&gt;
&lt;span class="n"&gt;validates_numericality_of&lt;/span&gt; &lt;span class="ss"&gt;:height&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:greater_than_or_equal_to&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;3.0&lt;/span&gt;
&lt;span class="n"&gt;validates_numericality_of&lt;/span&gt; &lt;span class="ss"&gt;:height&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:less_than_or_equal_to&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;:greater_than&lt;/code&gt; - Specifies the value must be greater than the supplied value. The default error message for this option is &lt;em&gt;"must be greater than %{count}"&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:greater_than_or_equal_to&lt;/code&gt; - Specifies the value must be greater than or equal to the supplied value. The default error message for this option is &lt;em&gt;"must be greater than or equal to %{count}"&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:equal_to&lt;/code&gt; - Specifies the value must be equal to the supplied value. The default error message for this option is &lt;em&gt;"must be equal to %{count}"&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:less_than&lt;/code&gt; - Specifies the value must be less than the supplied value. The default error message for this option is &lt;em&gt;"must be less than %{count}"&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:less_than_or_equal_to&lt;/code&gt; - Specifies the value must be less than or equal to the supplied value. The default error message for this option is &lt;em&gt;"must be less than or equal to %{count}"&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:other_than&lt;/code&gt; - Specifies the value must be other than the supplied value. The default error message for this option is &lt;em&gt;"must be other than %{count}"&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:in&lt;/code&gt; - Specifies the value must be in the supplied range. The default error message for this option is &lt;em&gt;"must be in %{count}"&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:odd&lt;/code&gt; - Specifies the value must be an odd number if set to true. The default error message for this option is &lt;em&gt;"must be odd"&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:even&lt;/code&gt; - Specifies the value must be an even number if set to true. The default error message for this option is &lt;em&gt;"must be even"&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;# Validates_inclusion_of&lt;/strong&gt; checks to see if an attribute is in a list of choices, and you need to provide it an array or a range for it to look in.&lt;br&gt;
&lt;strong&gt;# Validates_exclusion_of&lt;/strong&gt; checks to see if it is not present in the list of choices&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;validates_inclusion_of&lt;/span&gt; &lt;span class="ss"&gt;:status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:in&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'new'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'created'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'approved'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'deleted'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;validates_exclusion_of&lt;/span&gt; &lt;span class="ss"&gt;:status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:in&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;55&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;66&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;77&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;# Validates_format_of check&lt;/strong&gt; if it matches regex&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;validates_format_of&lt;/span&gt; &lt;span class="ss"&gt;:zipcode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:with&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="sr"&gt;/\d{6}/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;# Validates_uniqueness_of&lt;/strong&gt; is going to make sure that an attribute is not already in the database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;validates_uniqueness_of&lt;/span&gt; &lt;span class="ss"&gt;:user_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:case_sensitive&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kp"&gt;false&lt;/span&gt;
&lt;span class="n"&gt;validates_uniqueness_of&lt;/span&gt; &lt;span class="ss"&gt;:teacher_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;scope: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:semester_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:class_id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
 &lt;span class="c1"&gt;# Making sure that a teacher can only be on the schedule once per semester for a particular class.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;# Validates_confirmation_of&lt;/strong&gt; Encapsulates the pattern of wanting to validate a password or email address field with a confirmation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;
 &lt;span class="n"&gt;validates_confirmation_of&lt;/span&gt; &lt;span class="ss"&gt;:user_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:password&lt;/span&gt;
 &lt;span class="n"&gt;validates_confirmation_of&lt;/span&gt; &lt;span class="ss"&gt;:email_address&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                              &lt;span class="ss"&gt;message: &lt;/span&gt;&lt;span class="s1"&gt;'should match confirmation'&lt;/span&gt;
&lt;span class="c1"&gt;# The added password_confirmation attribute is virtual; it exists only as an in-memory attribute for validating the password. To achieve this, the validation adds accessors to the model for the confirmation attribute.&lt;/span&gt;
&lt;span class="c1"&gt;# NOTE: This check is performed only if password_confirmation is not nil. To require confirmation, make sure to add a presence check for the confirmation attribute:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;# Validates_associated&lt;/strong&gt; is used to check associations with other models, so it check the validity of the associated model&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Library&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
    &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:books&lt;/span&gt;
    &lt;span class="n"&gt;validates_associated&lt;/span&gt; &lt;span class="ss"&gt;:books&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;# Validates_acceptance_of&lt;/strong&gt; is typically used when the user needs to agree to your application's terms of service, confirm that some text is read, or any similar concept. This method creates a virtual attribute and it is not saved to the database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;validates_acceptance_of&lt;/span&gt; &lt;span class="ss"&gt;:terms_of_service&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;# Validate multiple parameters&lt;/strong&gt; in one go&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;validates&lt;/span&gt; &lt;span class="ss"&gt;:attr_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="ss"&gt;:presence&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="ss"&gt;:numericality&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="ss"&gt;:length&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;option_hash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="ss"&gt;:format&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:with&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="ss"&gt;:inclusion&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:in&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;range_or_array&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="ss"&gt;:exclusion&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:in&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;range_or_array&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="ss"&gt;:acceptance&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="ss"&gt;:uniqueness&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="ss"&gt;:confirmation&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;boolean&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Custom Validations
&lt;/h2&gt;

&lt;p&gt;Most of the time, the built-in validations that Ruby on Rails gives you, are going to be enough to help you keep your data under control. But every now and then there's some special cases that come up where they're not enough and we'd like to be able to write our own custom validations. Well, Ruby on Rails lets us do that too.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;validate&lt;/span&gt; &lt;span class="ss"&gt;:custom_method&lt;/span&gt;

&lt;span class="kp"&gt;private&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;custom_method&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;test_case&lt;/span&gt;
        &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:attribute&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"error message"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;# You can use :base as attribute name if the error is not specific to an attribute &lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Skipping Validation
&lt;/h2&gt;

&lt;p&gt;Skipping validations bypasses the data rules that you've established for the application and it should be done with extreme caution because you could actually introduce invalid data into the database and that could have severe consequences to the application.&lt;br&gt;
Here are few ways to do that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;validate: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update_columns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# This directly updates the database&lt;/span&gt;
&lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update_column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you have an active record relation, you've built up like a relation defined using where clauses to find the record you want, then you can call, update all and provide a hash of values and it will go and do update columns ****on all of those records without actually finding them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;relation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update_all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Thanks For Reading, Follow Me For More&lt;br&gt;
Share your suggestions, doubts and feedback in the comments section!&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>rails</category>
      <category>activerecord</category>
      <category>ruby</category>
      <category>database</category>
    </item>
  </channel>
</rss>
