Simplify object and association checks

* check for <object> instead of <object>.nil?
* check for <association>_id? instead of <association>.nil? (avoids
  record loading)
This commit is contained in:
2025-01-16 20:42:18 +01:00
parent c908063212
commit 7962cdf169
13 changed files with 24 additions and 26 deletions

View File

@@ -7,8 +7,8 @@ class Quantity < ApplicationRecord
dependent: :restrict_with_error
validate if: ->{ parent.present? } do
errors.add(:parent, :user_mismatch) unless user == parent.user
errors.add(:parent, :self_reference) if self == parent
errors.add(:parent, :user_mismatch) unless user_id == parent.user_id
errors.add(:parent, :self_reference) if id == parent_id
end
validate if: ->{ parent.present? }, on: :update do
errors.add(:parent, :descendant_reference) if ancestor_of?(parent)
@@ -68,7 +68,7 @@ class Quantity < ApplicationRecord
numbered.project(
numbered[Arel.star],
numbered.cast(numbered[:child_number], 'BINARY').as('path'),
).where(root.nil? ? numbered[:parent_id].eq(nil) : numbered[:id].eq(root.id)),
).where(root ? numbered[:id].eq(root.id) : numbered[:parent_id].eq(nil)),
numbered.project(
numbered[Arel.star],
arel_table[:path].concat(numbered[:child_number]),