<?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>DEV Community: Spoorthi</title>
    <description>The latest articles on DEV Community by Spoorthi (@skg0811).</description>
    <link>https://dev.to/skg0811</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%2F894466%2F5ee4c64d-0ef3-4864-8bd4-ed9950457f1e.png</url>
      <title>DEV Community: Spoorthi</title>
      <link>https://dev.to/skg0811</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/skg0811"/>
    <language>en</language>
    <item>
      <title>Comparison Validator in Rails 7</title>
      <dc:creator>Spoorthi</dc:creator>
      <pubDate>Thu, 01 Sep 2022 11:00:40 +0000</pubDate>
      <link>https://dev.to/skg0811/comparison-validator-in-rails-7-3493</link>
      <guid>https://dev.to/skg0811/comparison-validator-in-rails-7-3493</guid>
      <description>&lt;p&gt;Comparison with another &lt;strong&gt;value&lt;/strong&gt;, &lt;strong&gt;proc&lt;/strong&gt;, or &lt;strong&gt;attribute&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;ActiveRecord Validations are the most widely used and well-known functionality in Rails. It provides a complete validation framework to validate the state of an object before it is saved in the database. There are various ActiveRecord Validators like &lt;code&gt;presence&lt;/code&gt; , &lt;code&gt;uniqueness&lt;/code&gt; , &lt;code&gt;length&lt;/code&gt; and so on…&lt;/p&gt;

&lt;p&gt;If you had a scenario of validating an attribute with some value, in the older versions of Rails we had to write our own custom validations for the same. However, Rails 7 has now added &lt;code&gt;ComparisonValidtor&lt;/code&gt;. This provides us with an easy way to validate a comparison between two comparable values. This validator requires an option[&lt;code&gt;value&lt;/code&gt;, &lt;code&gt;proc&lt;/code&gt;, or &lt;code&gt;symbol&lt;/code&gt;] to be supplied for comparing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--db0k4kOE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dljzwz7rif8pz6s32h72.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--db0k4kOE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dljzwz7rif8pz6s32h72.png" alt="Comparison" width="880" height="794"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's assume we have a model appointment where we have attributes &lt;code&gt;start_date&lt;/code&gt; and &lt;code&gt;end_date&lt;/code&gt; along with others.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Before&lt;/strong&gt;&lt;br&gt;
If you had to validate &lt;code&gt;end_date&lt;/code&gt; attribute for any provided value or another attribute, we had to write the custom validation as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Appointment &amp;lt; ApplicationRecord

  validates :start_date, presence: true
  validates :end_date, presence: true
  validate :end_date_is_after_start_date
  def end_date_is_after_start_date
    if end_date &amp;lt; start_date
      errors.add(:end_date, 'can not be before the start 
                 date')
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;With Rails 7&lt;/strong&gt;&lt;br&gt;
You don't need to use any custom validation and the ComparisonValidtor comes to the rescue. Let's rewrite using the comparison&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Appointment &amp;lt; ApplicationRecord
  validates :start_date, presence: true
  validates :end_date, presence: true
  validates :end_date, comparison: { greater_than: :start_date }
  # OR
  validates_comparison_of :end_date, greater_than: :start_date
end

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ComparisonValidator provides support to the below-mentioned options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;greater_than&lt;/code&gt; : Specifies that the value must be greater than the supplied value. The default error message would be “must be greater than %{value}”.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;greater_than_or_equal_to&lt;/code&gt; : Specifies that the value must be greater or equal to the supplied value. The default error message is “must be greater than or equal to %{value}”.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;equals_to&lt;/code&gt; : Specifies that the value should be equal to the supplied value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;less_than&lt;/code&gt; : Specifies that the value should be less than the supplied value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;less_than_or_equal_to&lt;/code&gt; : This signifies that the value should be less than or equal to the supplied value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;other_than&lt;/code&gt; : Specifies that the value must be other than the supplied value. The default error message is “must be other than the %{value}”&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Can we combine multiple Compare options?&lt;/strong&gt;&lt;br&gt;
Yes, absolutely! We can combine multiple compare options.&lt;/p&gt;

&lt;p&gt;Let's say that the end_date must be greater than the start_date and also, wants to validate that the end_date is not today.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Appointment &amp;lt; ApplicationRecord
  validates :start_date, presence: true
  validates :end_date, presence: true
  validates_comparison_of :end_date, greater_than: 
   :start_date, other_than: Date.today
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Is ComparisonValidator only for date comparison?&lt;/strong&gt;&lt;br&gt;
Absolutely no, we can compare &lt;code&gt;numeric&lt;/code&gt; , &lt;code&gt;date&lt;/code&gt; and as well as the &lt;code&gt;string&lt;/code&gt; values with the ComparisonValidator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Author &amp;lt; ApplicationRecord
   validates_comparison_of :books_count, greater_than: 5
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ComparisonValidator is definitely a great addition to Rails 7. As it allows us to compare the data without being to write the custom validations and validates the data effortlessly.&lt;/p&gt;

&lt;p&gt;Happy Coding!!❤️&lt;/p&gt;

</description>
      <category>rails</category>
      <category>activerecordvalidators</category>
      <category>ruby</category>
    </item>
  </channel>
</rss>
