[Ruby on Rails] Implementation of validation that works only when the conditions are met

In Ruby on Rails, there is a scene where you want to write a validation that works only when a certain condition is met, and it took time to solve it, so the following shows how to write a validation that works only when a certain condition is met.

Overview

Description of validation that is valid only when the value of column X of the record you want to register is a

Concrete example

In the example below, when the value of column public_flag (type: integer) of products table is 1, This is an example of imposing a restriction that the column product name (name), product description (description), and product price (price) of the products table cannot be registered if they are empty.

In the following cases, if the value of public_flag is 0, it is possible to register in the products table even if the product name (name), product description (description), and product price (price) values are empty.

model(product.rb)

 with_options presence:true, if: :isProductPublicable?  do |v|
   v.validates :name
   v.validates :description
   v.validates :price
 end

 #public_true when flag is 1
 def isProductPublicable?
  public_flag == 1
 end

with_options is an option that allows you to apply multiple validations at once. isProductPublicable? Returns true if the value of column public_flag is 1.

Recommended Posts

[Ruby on Rails] Implementation of validation that works only when the conditions are met
(Ruby on Rails6) Display of the database that got the id of the database
part of the syntax of ruby ​​on rails
[Ruby on rails] Implementation of like function
[Rails] Implementation of validation that maintains uniqueness
List of copy and paste collections that are useful when creating apps with Ruby on Rails
Ruby on Rails When you don't know the cause of rollback when saving.
Implementation of Ruby on Rails login function (Session)
[Ruby on Rails] Until the introduction of RSpec
When the Ruby on Rails terminal rolls back
[Ruby on Rails] Implement a pie chart that specifies the percentage of colors
Ruby on Rails <2021> Implementation of simple login function (form_with)
Implementation of Ruby on Rails login function (devise edition)
Docker the development environment of Ruby on Rails project
[Ruby on Rails] Implementation of tagging function/tag filtering function
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
Try using the query attribute of Ruby on Rails
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
[Ruby On Rails] Description that allows only specific users to transition to the edit page
Ruby on Rails validation summary
[Ruby on Rails] Only the user who posted can edit
Delete all the contents of the list page [Ruby on Rails]
A note about the seed function of Ruby on Rails
[Note] About the Fizz_Buzz problem (How Ruby on Rails works)
Basic knowledge of Ruby on Rails
[Ruby on Rails] Change the save destination of gem refile * Note
[Ruby on Rails] Posting function that only logged-in users can post
[Ruby on Rails] Comment function implementation
Incident (rails) that is logged out when user editing of the application
The problem that the contents of params are completely displayed in the [Rails] view
How to solve the local environment construction of Ruby on Rails (MAC)!
How to debug the processing in the Ruby on Rails model only on the console
[Ruby On Rails] How to search the contents of params using include?
[Ruby on Rails] Introduction of initial data
[Rails] Addition of Ruby On Rails comment function
Let's summarize "MVC" of Ruby on Rails
[Ruby on Rails] Follow function implementation: Bidirectional
[Ruby on Rails] Japanese notation of errors
Explanation of Ruby on rails for beginners ①
[Ruby on Rails] How to make the link destination part of the specified id
[Ruby on Rails] Solving the addiction when setting crontab using whenever in EC2
How to resolve errors that occur in the "Ruby on Rails" integration test
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
[Java] Where is the implementation class of annotation that exists in Bean Validation?
[Rails] Set validation for the search function using Rakuten API (from the implementation of Rakuten API)
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
[Rails] Implementation procedure when public / private functions are added to the posting function
Validation settings for Ruby on Rails login function
Recommendation of Service class in Ruby on Rails
Publish the app made with ruby on rails
Ruby on Rails ~ Basics of MVC and Router ~
[Ruby on Rails] A memorandum of layout templates
Ruby on Rails Email automatic sending function implementation
Determine the current page with Ruby on Rails
Ruby on Rails address automatic input implementation method
[Ruby on Rails] Individual display of error messages
ArchUnit practice: Enforce package private visibility of classes that depend only on the same package
[Ruby on Rails] If there are duplicate records when registering data, do not register
[Ruby on rails] When executing the heroku command, bash: heroku: command not found is displayed. [Rails tutorial]
Library not loaded when trying to upgrade the version of ruby and rails s
[Ruby On Rails] Correct description location of unique constraint that gives uniqueness to DB