RailsCasts Pro episodes are now free!
Learn more or hide this
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.
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.
Any idea on how to implement with postgresql
order("id desc").group("order_id").having(state: state)
and the relative scope in Order model
This worked for me:
joins(:events).where("order_events.id IN (SELECT MAX(id) FROM order_events GROUP BY order_id) AND state = 'open'")
Building on your response, I replaced Ryan's with_last_state with this MAX(id) solution:
def self.with_last_state state
where state: state, id: select('MAX(id)').group(:order_id)
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...
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:
joins(:events).where("user_events.id IN (SELECT MAX(id) FROM user_events GROUP BY user_id) AND state = 'open'")
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.
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.
Very easy to understand the material! Thanks to the author!
Very easy to understand the material
First sign in through GitHub to post a comment.