# Act\_as\_state\_machine question

**URL:** <https://www.ruby-forum.com/t/act-as-state-machine-question/165839>\
**Category:** Rails\
**Created:** [April 18, 2009, 1:57am UTC](https://www.ruby-forum.com/t/act-as-state-machine-question/165839 "2009-04-18T01:57:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [April 18, 2009, 1:57am UTC](https://www.ruby-forum.com/t/act-as-state-machine-question/165839/1 "2009-04-18T01:57:00Z")

</div>

If I am in a form and I am editing it and I have a status of say  
“being edited”. If someone were to navigate away say to show all  
forms or something is there a way to update the state of the form?

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [April 18, 2009, 2:29am UTC](https://www.ruby-forum.com/t/act-as-state-machine-question/165839/2 "2009-04-18T02:29:08Z")

</div>

Me wrote:

> If I am in a form and I am editing it and I have a status of say  
> “being edited”. If someone were to navigate away say to show all  
> forms or something is there a way to update the state of the form?

That is generally not what a state machine is for. Your states should  
have a  
wider granularity, and users should push records into new states by  
explicitly  
submitting forms.

Even if you wrote an Ajax call in the “onblur” event of your form (if  
there is  
one), and even if the user’s browser did not reject the call as an  
attempt to  
pop up an ad, your user might simply unplug from the grid suddenly.

To prevent users from editing the same record, do what Wikis do. Send  
out one  
form with a timestamp. When the POST comes back, compare the timestamp  
to the  
record’s current stamp. If it has changed, inform the user they should  
use the  
Back button to retrieve their changes, they should copy them out to a  
Notepad,  
and then they should refresh their record to see the latest version.

–  
Phlip  
[http://flea.sourceforge.net/resume.html](http://flea.sourceforge.net/resume.html)

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [April 18, 2009, 2:39am UTC](https://www.ruby-forum.com/t/act-as-state-machine-question/165839/3 "2009-04-18T02:39:05Z")

</div>

OK, I have a status called "being\_edited when a record is being updated.  
When this happens I removed the ability to edit the record from the SHOW  
screen. Ok so I guess if in the edit screeen do not have a navigation  
link?

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [April 18, 2009, 2:56am UTC](https://www.ruby-forum.com/t/act-as-state-machine-question/165839/4 "2009-04-18T02:56:02Z")

</div>

Robert W. wrote:

> Phlip wrote:

> > To prevent users from editing the same record, do what Wikis do. Send  
> > out one  
> > form with a timestamp. When the POST comes back, compare the timestamp  
> > to the  
> > record’s current stamp. If it has changed, inform the user they should  
> > use the

> Is there some reason to go through all this trouble with using  
> timestamps? Why not just use optimistic locking? I’m not saying this  
> isn’t necessary, I’m actually curious of any advantage of using  
> timestamps.

Fail open instead of fail closed?

But I am more concerned about Chris’s reply - I can’t figure out the  
question…

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [April 18, 2009, 3:34am UTC](https://www.ruby-forum.com/t/act-as-state-machine-question/165839/5 "2009-04-18T03:34:52Z")

</div>

I just do this in my application.html page:

\<% unless @mop.state.eql?(“Being\_Edited”) %\>  
\<%= link\_to “New Mop”, :action =\> “new” %\>  
\<%= link\_to “Review Mop”, :action =\> “review” %\>  
\<% end %\>

Same thing really for the Show page, I just put an unless condition to  
remove the “edit” link if the mop is being edited or looked at.

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [April 18, 2009, 2:44am UTC](https://www.ruby-forum.com/t/act-as-state-machine-question/165839/6 "2009-04-18T02:44:53Z")

</div>

Phlip wrote:

> To prevent users from editing the same record, do what Wikis do. Send  
> out one  
> form with a timestamp. When the POST comes back, compare the timestamp  
> to the  
> record’s current stamp. If it has changed, inform the user they should  
> use the

Is there some reason to go through all this trouble with using  
timestamps? Why not just use optimistic locking? I’m not saying this  
isn’t necessary, I’m actually curious of any advantage of using  
timestamps.

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [April 18, 2009, 5:55am UTC](https://www.ruby-forum.com/t/act-as-state-machine-question/165839/7 "2009-04-18T05:55:50Z")

</div>

Phlip wrote:

> > remove the “edit” link if the mop is being edited or looked at.  
> > What prevents the user from typing the url by hand to edit the action?  
> > Then you’d have two users with the same form in the edit state. I don’t  
> > think hiding the link is really preventative.

Cheers,  
Darrik

–  
Darrik Mazey  
DMT Programming, LLC.  
P.O. Box 91  
Torrington, CT 06790  
office: 330.983.9941  
fax: 330.983.9942  
mobile: 330.808.2025  
removed\_email\_address@domain.invalid

To obtain my public key, send an email to  
removed\_email\_address@domain.invalid.

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [April 18, 2009, 6:36am UTC](https://www.ruby-forum.com/t/act-as-state-machine-question/165839/8 "2009-04-18T06:36:10Z")

</div>

Nothing really but the people who use this will most likely not know how  
to  
do that. Is there a better way to prevent someone from editing a form  
while  
someone else has it open? I saw something about optimistic locking but  
not  
quite sure what it really is or how to use it.

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [April 18, 2009, 4:21am UTC](https://www.ruby-forum.com/t/act-as-state-machine-question/165839/9 "2009-04-18T04:21:11Z")

</div>

Chris H. wrote:

> I just do this in my application.html page:
> 
> \<% unless @mop.state.eql?(“Being\_Edited”) %\>  
> \<%= link\_to “New Mop”, :action =\> “new” %\>  
> \<%= link\_to “Review Mop”, :action =\> “review” %\>  
> \<% end %\>
> 
> Same thing really for the Show page, I just put an unless condition to  
> remove the “edit” link if the mop is being edited or looked at.

As a View style thing, a button should not just disappear if it wouldn’t  
have worked. Let the user click on the button, then provide a timely  
error  
message.

How about you disable the link (make it grey, not ), and then put a  
message next to it “Asok started editing this at 3:11”? Can someone  
think of  
a way to do that, with a command override if Asok went home and left his  
browser running?
