#392 A Tour of State Machines pro
Here I show how three popular state machine gems can be used to clean up a list of boolean/datetime fields. I also show a custom solution which keeps track of the history of events.
- Download:
- source code
- mp4
- m4v
- webm
- ogv
Thanks Ryan. Good review.
What bothers me about most of these is that they keep state behavior all in one class.
That can make for a big state model class.
An alternative is something like StateObjects
which uses the State Design Pattern from the Gang of Four book. The state behavior is moved to small separate classes. StateObjects has less support for transitions/events but more for extracting the behavior.
http://blog.envylabs.com/post/29045466981/the-rails-state-machine
probably this would be nice too in the show notes.
it seems to me that not very much people know about the built-in version.
just used it once. i dont even know if its also built-in in the current version of rails
ok seems it got lost in 3.x ;) sorry for bothering
It has been extracted to transitions gem, even before rails 3 came out.
Is the Atom feed broken, or am I missing something?
I haven't received any updates in google Reader since Turbolinks. Looking at the XML at http://feeds.feedburner.com/railscasts, it hasn't been updated since then either.
The feeds only show the free ones. Everything since Turbolinks has been revised or pro.
The RSS at the top right of the page will give you a personalised link containing the pro episodes.
http://railscasts.com/subscriptions/...../episodes.rss
Any idea on how to implement with postgresql
and the relative scope in Order model
This worked for me:
Building on your response, I replaced Ryan's
with_last_state
with thisMAX(id)
solution:Any luck with this?
I just removed the grouping and it works fine, not sure what purpose the group("order_id") was serving, maybe I am missing something?
Guys, do you know how to after updating object always change state to awaiting_review? I am using workflow gem.
Unlike AASM and Workflow, state_machine supports multiple state machines at same object/class.
Implementing the roll your own method of this for a project of mine and I keep getting an error when I attempt to find all "draft" or "open" or "whatever" orders:
ERROR: column "orders.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "orders".* FROM "orders" INNER JOIN "order_events" ON...
Any ideas?
Yeah - I'm experiencing this too. I tweaked the code but now it appears to finds all the orders that are both in that state currently, or have at some point been in that particular state, which isn't quite working either.
Did you solve this issue?
I ended up using:
Hope that helps.
Were you able to solve this?
I use the old hash syntax for state transitions for the same reason. An good example of where language syntax flexibility is neat.
Wow, this is great.
I was spending so much time trying to get my head around that exact problem (tracking order status). I felt like it was getting way more complicated than it should be.
Never realised state machines were so practical.
For an introduction to state machines, I would highly recommend this series of lectures: http://www.youtube.com/watch?v=HyUK5RAJg1c
It's fun to watch, and he goes through enough examples to make the concept very familiar by the time you get through it.
+1
+1
Is there any way to create custom workflows for each user? So a user may design his own workflow, and an object goes through the states.
I need the same thing!
Information could be stolen that may really hurt your business, so make sure that something that is amazingly sensitive is securely guaranteed in locked file cabinets which all computers are password protected.
Great information thanks!