Uniqueness validation in database when validation has a condition
问题 Using uniqueness validations in Rails is not safe when there are multiple processes unless the constraint is also enforced on the database (in my case a PostgreSQL database, so see this blog post). In my case, the uniqueness validation is conditional: it should only be enforced if another attribute in the model becomes true. So I have class Model < ActiveRecord::Base validates_uniqueness_of :text, if: :is_published? def is_published? self.is_published end end So the model has two attributes: