'Fasterer yields "Calling argumentless methods within blocks is slower than using symbol to proc" within my validations. How to solve it?
I am coding my own ERP. For the People
(model class) I have the following validations:
class People < ApplicationRecord
# some code for N:M relations
# validations
validates :aka, presence: { if: proc { |person| person.aka? } },
uniqueness: true,
length: { within: 3..25,
if: proc { |person| person.aka? } }
validates :last_name, presence: { if: proc { |person| person.last_name? } },
uniqueness: { scope: %i[last_name first_name] },
length: { within: 2..100,
if: proc { |person| person.last_name? } }
validates :phone_ext, presence: { if: proc { |person| person.phone_ext? } },
length: { within: 1..10,
if: proc { |person| person.phone_ext? } },
format: { with: /\A\d{1,10}\Z/i,
if: proc { |person| person.phone_ext? } }
validates :first_name, presence: true,
uniqueness: { scope: %i[last_name first_name] },
length: { within: 2..100 }
end
As you can see in all the if: proc { ....
lines, they are almost kind of the same stuff. And fasterer knows about it, that's why I am getting the Calling argumentless methods within blocks is slower than using symbol to proc message.
Now, unsuccessfully all day I have trying all day long to figure a way out to solve this Fasterer's message. I have tried lambdas, closures, &:, ->, so I give up.
Any ideas?
Solution 1:[1]
This is referring to, for example, a.map(&:foo)
being faster than a.map { |o| o.foo }
.
In this context, validates
will take a method name to check as a symbol. For example, if: :aka?
instead of if: proc { |person| person.aka? }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Schwern |