sts sts - 1 month ago 7
Ruby Question

Validation issue using multiple Proc objects

I have validation with

Proc
and problem with it

validates :subcategory, presence: true,
if: Proc.new {|product| product.detail.blank?} && Proc.new { |product| product.category.id != 16 }


My problem is when i have
true
in second proc my validation fires.

Why is it so? Should it not return
false
because
false && true=>false
?

Answer

Others have commented on better ways to implement what you want. I'll just point out the misunderstanding you have.

it should return false because false && true => false

Nope. Actually, it's Proc && Proc => Proc (proc1 && proc2 => proc2). So your first proc is ignored due to how && operator works (this happens at class loading time) and is never called, not even once.