# Setting a callback whose action depends on the previous stat

**URL:** <https://www.ruby-forum.com/t/setting-a-callback-whose-action-depends-on-the-previous-stat/108836>\
**Category:** Rails\
**Created:** [July 7, 2007, 11:50pm UTC](https://www.ruby-forum.com/t/setting-a-callback-whose-action-depends-on-the-previous-stat/108836 "2007-07-07T23:50:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![maurice-gladwell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8edcca/32.png) [@maurice-gladwell](https://www.ruby-forum.com/u/maurice-gladwell)\
**Post date:** [July 7, 2007, 11:50pm UTC](https://www.ruby-forum.com/t/setting-a-callback-whose-action-depends-on-the-previous-stat/108836/1 "2007-07-07T23:50:38Z")

</div>

I’ve been reading about ActiveRecord::Callbacks and they’re really  
cool. But I have a complex need I’m not sure they answer.

Let’s say we have a User model. User has a #status attribute, which  
can be either “active”, “closed” or “blocked”.

I want to set a callback to perform an action if an “active” User has  
been blocked. So I need to know:

1. That the user has at first been in “active” status.
2. That his status has been set to “blocked”.

I need to set the callback when someone tries to save him in this  
state, so to sum up:

I need to run some logic when someone updates a user.status from  
‘active’ to ‘blocked’.

Any idea how?

–  
Maurice B. Gladwell

---

<div class="post-metadata">

**Author:** ![maurice-gladwell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8edcca/32.png) [@maurice-gladwell](https://www.ruby-forum.com/u/maurice-gladwell)\
**Post date:** [July 8, 2007, 6:56pm UTC](https://www.ruby-forum.com/t/setting-a-callback-whose-action-depends-on-the-previous-stat/108836/2 "2007-07-08T18:56:55Z")

</div>

On Jul 7, 2007, at 5:49 PM, Maurice G. wrote:

> state, so to sum up:
> 
> I need to run some logic when someone updates a user.status from  
> ‘active’ to ‘blocked’.
> 
> Any idea how?
> 
> –  
> Maurice B. Gladwell

Thinking completely off the top of my head (e.g., none of this has  
actually been run), I’d say you could hook into the status writer and  
set a flag for later use in a before\_save callback.

before\_save :handle\_newly\_blocked

def status=(new\_status)  
if self.status == ‘active’ && new\_status == ‘blocked’  
@newly\_blocked = true  
end  
write\_attribute(:status, new\_status)  
end

def handle\_newly\_blocked  
if @newly\_blocked  
# … handle it …  
end  
end

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![maurice-gladwell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8edcca/32.png) [@maurice-gladwell](https://www.ruby-forum.com/u/maurice-gladwell)\
**Post date:** [July 8, 2007, 10:59pm UTC](https://www.ruby-forum.com/t/setting-a-callback-whose-action-depends-on-the-previous-stat/108836/3 "2007-07-08T22:59:27Z")

</div>

Maurice G. wrote:

> I’ve been reading about ActiveRecord::Callbacks and they’re really  
> cool. But I have a complex need I’m not sure they answer.
> 
> Let’s say we have a User model. User has a #status attribute, which  
> can be either “active”, “closed” or “blocked”.
> 
> I want to set a callback to perform an action if an “active” User has  
> been blocked. So I need to know:
> 
> 1. That the user has at first been in “active” status.
> 2. That his status has been set to “blocked”.
> 
> I need to set the callback when someone tries to save him in this  
> state, so to sum up:
> 
> I need to run some logic when someone updates a user.status from  
> ‘active’ to ‘blocked’.
> 
> Any idea how?
> 
> –  
> Maurice B. Gladwell

I don’t know how you are going to use this function,  
but maybe you’re going to use it this way.

A user presses the “block” button.  
this calls the “block!” method on the user,  
execute whatever code you need.

---

<div class="post-metadata">

**Author:** ![maurice-gladwell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8edcca/32.png) [@maurice-gladwell](https://www.ruby-forum.com/u/maurice-gladwell)\
**Post date:** [July 21, 2007, 6:42pm UTC](https://www.ruby-forum.com/t/setting-a-callback-whose-action-depends-on-the-previous-stat/108836/4 "2007-07-21T18:42:53Z")

</div>

Sorry for the late response, but I couldn’t resist telling you to look  
at the acts\_as\_state\_machine plugin. It lets you program transition  
logic like you’re describing declaratively.

On 7/7/07, Maurice G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1. That the user has at first been in “active” status.  
> –  
> Maurice B. Gladwell
> 
> >

–  
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=  
Pre-order my book The Rails Way  
[http://www.amazon.com/dp/0321445619](http://www.amazon.com/dp/0321445619)

blog: [Best Online Casino in Australia | Top Licensed Casinos for Gamblers](http://jroller.com/obie/)

---

<div class="post-metadata">

**Author:** ![maurice-gladwell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8edcca/32.png) [@maurice-gladwell](https://www.ruby-forum.com/u/maurice-gladwell)\
**Post date:** [July 8, 2007, 11:12pm UTC](https://www.ruby-forum.com/t/setting-a-callback-whose-action-depends-on-the-previous-stat/108836/5 "2007-07-08T23:12:52Z")

</div>

On 8-Jul-07, at 4:59 PM, Matthew R. wrote:

> > –  
> > Maurice B. Gladwell
> 
> I don’t know how you are going to use this function,  
> but maybe you’re going to use it this way.
> 
> A user presses the “block” button.  
> this calls the “block!” method on the user,  
> execute whatever code you need.

Although your example is a simple state machine, I am a big fan of  
acts\_as\_state\_machine.

[http://lunchroom.lunchboxsoftware.com/2006/1/21/acts-as-state-machine](http://lunchroom.lunchboxsoftware.com/2006/1/21/acts-as-state-machine)

Jodi
