Problem with validation

24 views
Skip to first unread message

Gm

unread,
Apr 29, 2015, 10:52:06 AM4/29/15
to rubyonra...@googlegroups.com
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.

Chris Lerum

unread,
Apr 29, 2015, 11:52:55 AM4/29/15
to rubyonra...@googlegroups.com
It may help if you showed us this code of which you speak.

Gm

unread,
Apr 29, 2015, 11:58:39 AM4/29/15
to rubyonra...@googlegroups.com
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.

Gm

unread,
Apr 29, 2015, 12:14:54 PM4/29/15
to rubyonra...@googlegroups.com
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

Colin Law

unread,
Apr 29, 2015, 12:15:08 PM4/29/15
to rubyonra...@googlegroups.com
On 29 April 2015 at 16:58, Gm <javap...@gmail.com> 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

Colin Law

unread,
Apr 29, 2015, 12:18:53 PM4/29/15
to rubyonra...@googlegroups.com
On 29 April 2015 at 17:14, Gm <javap...@gmail.com> 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
Reply all
Reply to author
Forward
0 new messages