True validation for uniqueness
Every rubist who worked from Ruby on Rails familiar with 3r336. ORM 3r382. ActiveRecord . Let us discuss one of the validations proposed from the box, namely, validation for uniqueness, and why database_validations gem will save the consistency of your database.
3r314.
Suppose you have a user model with uniqueness on the field email i.e.
3r3108. class User < ApplicationRecord
validates: email, uniqueness: true
end
3r3117. 3r3118.
You may already know that this validation performs the following request
3r340. SELECT 1 FROM users WHERE email = $ 1 3r3118.
every time we try to save a record to the database.
This approach has several drawbacks:
Firstly , the execution of an additional request, and in case if several validations for uniqueness are initialized in the model, the request will be executed for each of them. This is not efficient and also requires the presence of indices if we want these queries to be executed quickly.
Secondly This solution does not guarantee uniqueness due to the possible 3r3633. data race 3r382. . Several competitive operations can simultaneously find out about the absence of a specific record, in consequence of which, keep the same data.
Of course, it is possible to allow rare cases with data race by adding a unique constraint at the database level. But in this case, you will not get a validation error, the query to the database will simply fall and the entire transaction will be rolled back.
In this situation, gem will help. database_validations which provides compatibility between database constraints and validations.
After reading 3r379. 3r382. and 3r3381. benchmarks 3r3r82. , it can be concluded that this gem will speed up the process of storing records in the database at least in 3r3383. two [/b] times.
With support for databases such as PostgreSQL , SQLite , [i] MySQL 3r3394. and backward compatibility with validates_uniqueness_of
, the replacement process on validates_db_uniqueness_of
takes minutes.
A convenient matcher for RSpec is also present out of the box:
3r3108. specify do
expect (described_class)
.to validate_db_uniqueness_of (: field)
.with_message ('duplicate')
.with_where ('(some_field IS NULL)')
.scoped_to (: another_field)
.with_index (: unique_index)
end
3r3117. 3r3118.
When switching to a new validation, you need to have uniqueness restrictions in the database, but if they are not already present, gem will indicate this during the launch of the application.
The heme has been tested on an application with 100+ uniqueness validations among 50+ models.
Use gems and share opinions. Any contribution to further development is welcome! 3r3138.
3r3138.
It may be interesting
weber
Author27-11-2018, 22:13
Publication DateDevelopment / Ruby on Rails
Category- Comments: 0
- Views: 274
We take being #1 in Commercial Odor Remover Products & Systems very seriously. Here are some of the reasons you can trust Cupridyne Clean to bring you a solution to your Commercial Odor Control problem.