I have a unique constraint defined using a condition. But the following test fails:
class Dummy include Mongoid::Document field :name, :type => String field :status, :type => Boolean validates_uniqueness_of :name, if: :status end describe "UniquenessValidator" do let!(:d1) { Dummy.create!(name: 'NAME_1', status: true) } let!(:d2) { Dummy.create!(name: 'NAME_1', status: false) } it "should raise an error" do expect { d2.status = true d2.save! }.to raise_error end end
So how is name_changed? is false, validation does not occur, and therefore the condition of uniqueness is not verified.
This is mistake? Or am I forgetting something? I assume this is an optimization to avoid running a check every time an item has been changed.
In this case, what is a good way to initiate a check when the status changes?
Thanks!
ruby mongoid
Aymeric
source share