getting started with acts_as_state_machine

This is a note to self. Remember to define all states for an object before you start referencing it.

If not, you will get an error like this:
“You have a nil object when you didn’t expect it! The error occurred while evaluating nil.entering”

This is the solution:

1
2
3
4
5
6
7
8
9
10
11
class Squad < ActiveRecord::Base  
  acts_as_state_machine :initial => :in_progress
 
  event :complete do
    transitions :from => :in_progress, :to => :completed,  
                      :guard => Proc.new {|s| !s.completed_at.blank? }
   end

    state :in_progress # This is the line I had forgotten to write

    state :completed,  :enter => :do_complete

Have fun with acts_as_state_machine!

And keep in mind that acts_as_state_machine is not concurrent. Read more here.

This entry was posted in programming, Ruby programming, software engineering and tagged , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam protection by WP Captcha-Free