Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Change state of a database entry at a specific time

I am creating a system to manage deadlines. I store the deadline of a project in the table using a datetime column.

Now I can't figure out a way to change a column entry of a record at the time specified by the deadline entry.

I mean do we have to check the database in like every 5 minutes to see if the current time is equal to the deadline?

2 Answers

Brandon Barrette
Brandon Barrette
20,485 Points

This sounds difficult to do, you would need another system to be checking that. It would be easier to do the following:

When the project loads, check the deadline in the database. If it's after the deadline, then have a message displayed or restrict access to the project. This way, if a user clicks on a project that's after the deadline, you can redirect or display something else, limiting their access.

The other issue you would have to watch out for is if a user is editing the project and the deadline passes before they save their work, you would want to check that the deadline hasn't passed. The way I would do that is add a check before saving the project (this could be done in the model with a before_save)

Hope that makes sense. This is how I would do it with Rails. There are probably other methods, but this would be the easiest to implement.

Thanks for your help. For now I have implemented time checks in my views using 'Time.zone.now > deadline'. I am planning to implement a javascript countdown which counts down the time till deadline so that the user gets an idea of how much is remaining before the applications for the project close. And I think a simple check for time at the controller level will ensure that a user won't be able to hack through the javascript and change his timestamps or the deadlines and then still edit the project.