Introduction to state machines

09 Jan 2015

Yesterday I talked about state machines on the Berlin Ruby Usergroup.

Introduction to state machines

Rails has this nice little feature called Enums. The introduction example is something like this:

class Conversation < ActiveRecord::Base
  enum status: [ :active, :archived ]
end

And I think this is dangerous. States should be dealed with in a state machine. Why you ask? Because state changes usually have conditions attached to them. Only archive if ... . If you want to model something like that with enums, you end up with a horrible version of a state machine.

So let's see how we would do this in a "more cleaner way" with state machines.

comments powered by Disqus