Title
Acts As State Machine Variable “initial states” for an object
Body
look at the following model class using asm:


class User < ActiveRecord::Base
  acts_as_state_machine :initial => :pending
  state :pending
  state :enabled
  state :disabled

  event :enable do
    transitions :from => :pending, :to => :enabled
    transitions :from => :disabled, :to => :enabled
  end

  event :disable do
    transitions :from => :pending, :to => :disabled
    transitions :from => :enabled, :to => :disabled
  end
end
Here, normal user registration will cause initial state of 
user object to be a "pending" state; but when admin creates
 an user it should be "enabled", but asm will force the model 
to keep the state to be "pending".
We can achieve required solution by doing the following:--
def create
  User.write_inheritable_attribute :initial_state, :enabled
  user = User.new(params[:user])
  user.save
end

Cheers 
Alok Anand
Rails Detective
Associated Tags
Acts As state Machine, acts_as_state_machine, asm, initial_state, variable initial_state
Created By
alokanand
Created At
2011-12-16 10:35:56 UTC
Last Updated At
2011-12-16 10:35:56 UTC
Add Comment