Hi, I’m using a custom validation method to validate some logic.
But I realized that the database query is running two times.
So the database is being called twice.
The validation method just display an error if array items are
duplicated.
Log:
Questao Load (0.4ms)
[“a”, “a”]
Questao Load (0.4ms)
[“a”, “ab”]
What’s wrong with my code ?
Thanks.
It may help if you showed us this code of which you speak.
Hi Chris.
It’s just a simple validation method:
validate :custom
def custom
if self.text.blank? and self.persisted?
return errors.add(:base, ‘Error’)
else
end
Every custom method has this problem.
If I put a simple p “a” inside the method, two inspects are displayed:
(0.1ms) BEGIN
“a”
(0.1ms) ROLLBACK
(0.1ms) BEGIN
“a”
(0.1ms) ROLLBACK
On 29 April 2015 at 17:14, Gm [email protected] wrote:
If I put a simple p “a” inside the method, two inspects are displayed:
(0.1ms) BEGIN
“a”
(0.1ms) ROLLBACK
(0.1ms) BEGIN
“a”
(0.1ms) ROLLBACK
What code are you running that invokes the validation?
Colin
On 29 April 2015 at 16:58, Gm [email protected] wrote:
Hi Chris.
It’s just a simple validation method:
validate :custom
def custom
if self.text.blank? and self.persisted?
return errors.add(:base, ‘Error’)
else
end
You are missing an end statement so whatever follows after this will
be executed as part of the validation. Presumably you have an extra
end later on which allows it to run.
Colin