managing time sensitive data

I am not to sure how to go about doing this, and after any advice on
my problem.

As an example, i would like a similar feature to that of eBay where a
item will have an expiry time and then will be set as closed or etc,
once the time as finished.

So similar i would like a Question to have a week of being open then
after that week it will be set to different status of Resolved or
something. How would i go about checking for this? and making update
queries if the timer reaches zero?


Andrew Cetinick

The few times I've needed to do that, I started out thinking I needed something to actively "close" the record when the time was up. Not so (that can get quite complicated). All I actually needed was that any time a process tried to perform a task on that record, that task had to first see if the expiration time has passed. If so, the task interprets the record as closed, and acts accordingly.

If you have a lot of tasks that might happen, then you don't necessarily want to perform that calculation each time, so each task has the ability to mark a record as closed.

So, now the task process is to first check if the record is closed, if not, check if it has expired. If yes, then mark it as closed. Now subsequent tasks will only have to do the first check. I called it a "lazy close." Of course the code is abstracted out so tasks can use it w/o duplicating the code that does the work.

It may not be an appropriate strategy for all cases, but it worked for mine.

-- gw

thanks for your advice.

ye i was thinking of using an expiry date in the table, but im
confused on when i do the check and updates, cos as you said i wouldnt
want to calculate this each time i get a list of my questions.

"now the task process is to first check if the record is closed".
where would i go about implementing this method, will i do it in my
model for Question (Tasks in ur example )?
how would i implement this? im quite new to rails so dont know the
methods which could accomplish this.

One thought is to create a custom finder method in your model, then
use it in place of find. In that method, you could first do an
update_all on the database using conditions that only update "expired"
questions. This way you need not process any records manually and the
result is limited to a single database call for an arbitrary number of
records. Another option is to call the expiry update_all code from the
controllers before_filter. One note about update_all is that it does
not call any of the model callbacks nor does it check validation.

Hope this helps and I am sure there will be other ideas.


Much of the "how" depends on exactly what you're doing, and how sensitive the exactness of the timing is.

Are you writing a poll system?

-- gw

ye, a question answer system. question asked, goes into open status,
after one week then goes into expired, then set as resolved or

thanks william. ill try look into using the before_filter to try
implement it.

you could look at acts_as_draftable, REquires approval, and
acts_as_publishable plugins

Seems to me you just need a date column that represents the date and time that the question is considered closed. When you do queries you include a conditional element which identifies whether you want records where closedDate is > today or < today.

"show me open questions" would have a "WHERE closedDate >= today"

-- gw