The following post was contributed by Conrad Benham, Lead Consultant @ Cyrus
I like State Machine (SM). It encapsulates state. Its DSL acts to document the allowed states and associated transitions that may occur to move between those states. SM also validates transitions preventing invalid transitions from occurring. There are other things to like too. Real nice like!
As with things I like there can be things I dislike. Recently I had one of those days where those dislikes emerged. This is a heads up for you, should you find yourself using SM. SM does a number of things, one of them is to create question methods out of the states.
Imagine an ActiveRecord model with a field called accessibility. Accessibility has two SM managed states: "public" or "protected". SM doing what it does plonked two methods on my model: "public?" and "protected?". Lovely, now I can test if the object is public or protected by using those methods. This is great until you spend an hour or so figuring out why tests that were passing suddenly fail. Turns out I had a boolean column named 'public' on the same model object. Rails creates question methods for boolean columns. In this scenario SM won and imposed its public? method completely decimating the public? method introduced by AR (the one I truly cared for).
I had completely forgotten that SM created the question methods. I'm fully aware that AR does. I don't use SM as much as I use AR and after discovering another gotcha with the use of the question methods 6 months ago I vowed never to use them and promptly forgot about said gotcha...until now. Now they haunt my mind like a poltergeist.
The lesson: don't allow state to exist in any SM managed column that has the same name as a column in a table.
I still like SM. Now that I've fallen prey to this gotcha I'm sure I won't go there again and hopefully you'll know to avoid it yourself.