-
Notifications
You must be signed in to change notification settings - Fork 466
Open
Description
Hi! I try to validate a maximal depth, but it seems that Rails validations are skipped when cached depth is updated after a parent has been updated.
For example, given I want this maximal depth to be 1:
Node.has_ancestry(cache_depth: true)
Node.validates(:ancestry_depth, numericality: { less_than_or_equal_to: 1 })
a = Node.create! # ancestry_depth == 0
b = Node.create!(parent: a) # ancestry_depth == 1
c = Node.create!(parent: b) # error, as expected
x = Node.create! # ancestry_depth == 0
a.update!(parent: x) # it works, but then b.ancestry_depth == 2, which is not allowed
For now I've added a validation on update like:
def validate_descendants_ancestry_depth
return unless ancestry_changed?
return if descendants.maximum(:ancestry_depth) + ancestry_depth - ancestry_depth_was <= 1
errors.add(...)
end
But I was wondering if there was a built-in system for that? Like something checking if there is any other ActiveModel::Validations::NumericalityValidator
on the ancestry_depth
attribute?
Thanks! :)
Metadata
Metadata
Assignees
Labels
No labels