I am not too sure where exactly you are trying to execute this time
based procedure. It can be done on several different levels, each of
which would result in a different solution.
However, from the following statement I will assume that you might be
wanting to perform some duties on the model level:
“I want a countdown which is counting autonomus and starts if the user
logged in.”
Say that you are simply wanting to save an active record object every 5
minutes after the user has logged in. If the lifespan of an active
record object will be persistent throughout a user’s session, which I
would NOT advise, there is a really neat gem called the rufus scheduler
that might do the trick.
class Fish < ActiveRecord::Base
require ‘rubygems’
require ‘rufus/scheduler’
SCHEDULER = Rufus::Scheduler.start_new
def initialize
super
SCHEDULER.every "5m" do
#might want to consider the fact that this object could be in
a state where it can’t save due to validation constraints
self.save
end
end
end
Otherwise, you could simply check the “updated_at” timestamp for the
active record in the controller and run some code every 5 minutes
(current_time - updated_at > 5 minutes). Of course the drawback here is
that it is completely contingent upon the user making requests to the
server.
Otherwise, you could simply check the “updated_at” timestamp for the
active record in the controller and run some code every 5 minutes
(current_time - updated_at > 5 minutes). Of course the drawback here is
that it is completely contingent upon the user making requests to the
server.
Hey Aldo,
i ll try it with your controller solution, sounds great!
So my last problem is how can i display the user the countdown?
As i wrote i do it with some javascript countdown, but everytime the
user navigate on my site the countdown is reset.