Problem with validation

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.

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

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