Updating attributes as part of a state change?

51 views
Skip to first unread message

Robert Rawlins

unread,
Nov 26, 2012, 7:02:40 AM11/26/12
to pluginaw...@googlegroups.com
Hello Guys,

Brand spanking new to FSMs and State_Machine, so looking for some simple advice.

I have a simple model in an app which tracks customer service requests, the request can be in various states, open, assigned, resolved etc It's a pattern I'm sure you've all seen.

I have some attributes on the model which I want to set as part of an event, so when I call the 'close' event, I want the state to change, the 'closed_at' column to be updated to the current datetime and the 'closed_by' attribute to be set to the current user.

What is the best way of handling this? I couldn't find a way of referencing the instance from within the event :close block

Thanks for any help.

Rob

François Beausoleil

unread,
Nov 26, 2012, 8:38:05 AM11/26/12
to pluginaw...@googlegroups.com
Don't you receive the record, as in:

after_transition any => :closed do |record|
record.closed_at = Time.now.utc
end


Bye,
François

Robert Rawlins

unread,
Nov 26, 2012, 8:44:10 AM11/26/12
to pluginaw...@googlegroups.com
Hi François,

Thanks for the reply. I do indeed receive the record like that, I just wasn't sure if that was the best place to set it or not? If that's the best best practice then I'm happy doing it that way.

Thanks,

Robert

François Beausoleil

unread,
Nov 26, 2012, 9:57:11 AM11/26/12
to pluginaw...@googlegroups.com

Le 2012-11-26 à 08:44, Robert Rawlins a écrit :

> Hi François,
>
> Thanks for the reply. I do indeed receive the record like that, I just wasn't sure if that was the best place to set it or not? If that's the best best practice then I'm happy doing it that way.

That's how I've done it before. I would assume the after_transition are the best place to do those kinds of modifications.

The only thing I don't know / can't remember is if the attribute's value will be set before or after the transaction is closed.

Bye,
François

Robert Rawlins

unread,
Nov 27, 2012, 5:30:48 AM11/27/12
to pluginaw...@googlegroups.com
Thanks François, 

After a little playing around I actual realised that a single date field would not suffice as the case could be reopened at a later date.

I ended up using the State Machine Audit Trail gem https://github.com/wvanbergen/state_machine-audit_trail

I can then access the datetimes of any of the transition changes - which actually suits me much better.

Thanks for the advice.

Rob
Reply all
Reply to author
Forward
0 new messages