I asked a similar question here, Uniqueness check in the database, when the check has a condition , but my requirements have changed, so this question.
Using uniqueness checking in Rails is unsafe if there are several processes, if the restriction also does not apply in the database (in my case it is a PostgreSQL database, see http://robots.thoughtbot.com/the-perils-of-uniqueness-validations )
In my case, the uniqueness check is conditional: it should be used only if another attribute on the other model becomes true. So i
class Parent < ActiveRecord::Base
end
class Child < ActiveRecord::Base
belongs_to :parent
validates_uniqueness_of :text, if: :parent_is_published?
def parent_is_published?
self.parent.is_published
end
end
, Child : parent_id ( Parent) text ( ). Parent : is_published (boolean). text Child, parent.is_published .
, http://robots.thoughtbot.com/the-perils-of-uniqueness-validations, , is_published.
- "" PostgreSQL, ? , , , . ?