paranoid2 icon indicating copy to clipboard operation
paranoid2 copied to clipboard

Misbehaviour in "uniqueness" validation

Open loureirorg opened this issue 11 years ago • 4 comments

ActiveRecord's built-in uniqueness validation does not account for records deleted by paranoid2.

loureirorg avatar Aug 12 '13 05:08 loureirorg

a workaround is to use:

uniqueness: { conditions: -> { where(deleted_at: nil) } }

instead of:

uniqueness: true

loureirorg avatar Aug 12 '13 05:08 loureirorg

Uniqueness validator by default uses unscoped scope

Not sure if we need to change this. You won't be able to restore deleted record (it would be invalid)

yury avatar Aug 12 '13 07:08 yury

actasparanoid has solved this by adding a custom uniqueness validator.

its use:

validates_uniqueness_of_without_deleted :name

istead of:

validates_uniqueness_of :name

loureirorg avatar Aug 12 '13 07:08 loureirorg

@loureirorg you can use following scope to make code a little bit cleaner for now.

uniqueness: { conditions: -> { paranoid_scope } }

I will think more about this validator.

Thank you for your report

yury avatar Aug 12 '13 09:08 yury