Validate called twice - normal?

I'm playing around with ActiveScaffold and noticed my validations are
being called twice. I'm really not sure if this is a Rails problem or
something caused by ActiveScaffold, but is it normal that all the
validations callbacks
are being called twice on create/update. Take a simple model:

class Company < ActiveRecord::Base

        def validate_on_create
                puts "ON CREATE"
        end

        def validate
                puts "MAIN VALIDATE"
        end
end

I am seeing the following in the console upon create:

MAIN VALIDATE
ON CREATE
MAIN VALIDATE
ON CREATE

This is from a fresh install of InstantRails 1.7 with the latest
ActiveScaffold.

Nic Geadah wrote:

I'm playing around with ActiveScaffold and noticed my validations are
being called twice.

Ideally, there should be only two kinds of methods in the world:

  - queries?, which return a value without side-effects
  - commands!, which only have side-effects

In real life, some methods create both side-effects and return values.

However, we should strictly treat validations as the first kind. And all queries, such as valid?, are free to be called as many times as is convenient.

The scaffold probably has the equivalent of

   valid? and save!

for whatever reason. Pay it no mind, and write your validations without side-effects.

Thanks - and you are perfectly right about side-effects from validation
methods. My problem is not with undesirable side effects from the
validation method, but rather a performance concern. I validate the
contents of an uploaded file (which could be large) and I'd rather avoid
doing so much I/O twice. I guess I'm stuck creating a hack in my
validations to bypass it the "second time around".

Nic Geadah wrote:

Thanks - and you are perfectly right about side-effects from validation methods. My problem is not with undesirable side effects from the validation method, but rather a performance concern. I validate the contents of an uploaded file (which could be large) and I'd rather avoid doing so much I/O twice. I guess I'm stuck creating a hack in my validations to bypass it the "second time around".

Then that is absolutely not a "hack" - it is enforcing the contract of the query? interface.

Have you timed the process to see how long it takes?

And can you write unit tests that cover the situation? I would worry the data would get dirty and the validation would return its obsolete value. That's well over the threshold that I would test for.

If you've actually got validations like validates_uniqueness_of etc...
then they will get run multiple times if your model is loaded multiple
times

Fred