[Ruby] acts_as_state_machine question
Frederick Alger
fred at fredalger.net
Wed Nov 8 13:01:26 PST 2006
>> acts_as_state_machine sets the initial state in before_create, so I
>> tried using Order.create instead of Order.new, with the same result.
>
> I'm using it in a project and have passing unit tests.
>
> * As mentioned, state is not set until create, so use Order.create
Just had a great "aha!" and found what caused my acts_as_state Order
model to fail its unit test.
As we all know, before_create methods run just before the actual
insertion into the database, and after validations are run.
My Order model's validations fail with the default ActiveRecord-
supplied blank values (as they should!), so AR never gets to the
point of calling the before_create callback that acts_as_state
machine added to the model — the callback which sets the all-
important initial state of the machine. My state machines were
starting with state 'nil,' and this did not fly with acts_as_state
machine.
My solution?
class Order
# ...
def after_initialize
set_initial_state
end
# ...
end
Hope that saves someone some hair-pulling. =)
Thanks!
- Fred.
More information about the Ruby
mailing list