[RUBY] Use with_options to group the same validations together!

Introduction

I will summarize the validation used in the current application creation.

Summarize common validation

with_options

Taking frequently used validation as an example

model


validates :user, presence: true
validates :item, presence: true
validates :price, presence: true
validates :email, presence: true

It's just the same thing, it's getting longer and more readable ... In such a case, you can set validation collectively with with_options.

model


with_options presence: true do
  validates :user
  validates :item
  validates :price
  validates :email
end

Using with_options more advanced

Add more individual validations

model


with_options presence: true do
  validates :user, length: { minimum: 6 }
  validates :item
  validates :price, format: { with: /\A[0-9]+\z/ }
  validates :email
end

Validation can be added individually in do ~ end. By the way, the length option can limit the number of characters. The format option specifies a regular expression.

Further summarize

model


with_options presence: true do
  validates :user
  validates :item
  with_options uniqueness: true do
    validates :price
    validates :email
  end
end

You can nest with_options inside with_options. In the above example, presence: true is set to: user,: item,: price,: email, In addition, ʻuniqueness: true` is set to: price,: email. By the way, the uniqueness option prevents you from saving the same value. (Uniqueness)

Finally

Writing together with with_options is similar to the feeling of wrapping up in common terms per factorization learning that appeared in junior high school mathematics.

Recommended Posts

Use with_options to group the same validations together!
How to use the link_to method
How to use the include? method
How to use the form_with method
How to use the wrapper class
How to use MinIO with the same function as S3 Use docker-compose
How to use OrientJS and OrientDB together
[Java] How to use the File class
[Java] How to use the hasNext function
[Java] How to use the HashMap class
[Rails] How to use the map method
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
[Processing × Java] How to use the class
[Processing × Java] How to use the function
[Java] How to use the Calendar class
How to use @Builder and @NoArgsConstructor together
How to use the camera module OV7725 (ESP32-WROVER-B)
[Java] How to use Thread.sleep to pause the program
When you want to use the method outside
Output of how to use the slice method
How to use the replace () method (Java Silver)
[Ruby basics] How to use the slice method
Use SCNIK Constraint to freely animate the robot's arm
[Rails] I don't know how to use the model ...
[Beginner] Discover the N + 1 problem! How to use Bullet
[Swift] Use UserDefaults to save data in the app
Java: Use Stream to sort the contents of the collection
[Rails] Put together the same code with controller actions
How to use the same Mapper class in multiple data sources with Spring Boot + MyBatis