Gavin wrote:
> Anybody have any suggestions as to how I could go about this?
> 
> I was thinking of creating an observer for orders and including an
> after_create like so:
> 
> Class OrderObserver < ActiveRecord::Observer
> 
> def after_create
>   sleep 300 # sleep for 5 mins
>   if self.status > 3 # anything above 3 should be saved
>     return
>   else
>     self.items.each do |item|
>       item.update_attribute :order_id, nil
>     end
>     self.destroy
>   end
> end

Sorry if this response is a bit off the question's topic, but I do have 
an unrelated suggestion.

You have the following line in your sample code:
>   if self.status > 3 # anything above 3 should be saved

I would recommend against this use of "magic numbers," such as "3" in 
this case. The number 3 has no meaning here. I would recommend that you 
use something like a "finite state machine."

http://en.wikipedia.org/wiki/Finite_state_machine

There is a Ruby gem implementation of this:
http://github.com/rubyist/aasm/tree/master

Now the same line of code can be written as:
if self.completed?

Now there is no ambiguity on the meaning of some "magic number." The 
code clearly states its intent.
-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to