# How to do Daily Database Maintenance in a Rails Way?

**URL:** <https://www.ruby-forum.com/t/how-to-do-daily-database-maintenance-in-a-rails-way/105769>\
**Category:** Rails\
**Created:** [June 2, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/how-to-do-daily-database-maintenance-in-a-rails-way/105769 "2007-06-02T13:07:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@newbie](https://www.ruby-forum.com/u/newbie)\
**Post date:** [June 2, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/how-to-do-daily-database-maintenance-in-a-rails-way/105769/1 "2007-06-02T13:07:25Z")

</div>

I am learning Rails and came across the following problem.

I need to do some daily maintenance to the MySQL database. By  
maintenance, I mean reading data from one table, do some calculation,  
and put the result in another table. This should happen everyday at a  
specified time, e.g., at mid-night, no matter if there is any visitor  
visiting the site or not. Ideally this should happen quietly in the  
background without any human intervention.

How can I do it in Rails?

I understand that in some other environments like PHP I could do it in  
at least two ways:

1. Write a stored procedure for the daily maintenance;
2. Write a standalone script for the daily maintenance and use cron to  
schedule it to run once per day;

But these two are pretty un-Rails. So what should I do for tasks like  
this in a Rails way?

Thanks!

---

<div class="post-metadata">

**Author:** ![newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@newbie](https://www.ruby-forum.com/u/newbie)\
**Post date:** [June 2, 2007, 4:47pm UTC](https://www.ruby-forum.com/t/how-to-do-daily-database-maintenance-in-a-rails-way/105769/2 "2007-06-02T16:47:43Z")

</div>

When I’ve had to do this sort of thing in the past, I’ve always created  
a controller that had actions that did the maintenance. Then, in order  
to make it happen in the background, I would create a cron job that used  
wget to request that controller/action. This gives you a lot of  
flexibility.

The other option is to use script/runner and stick that in a cron job.  
Whatever floats your boat, really.

---

<div class="post-metadata">

**Author:** ![newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@newbie](https://www.ruby-forum.com/u/newbie)\
**Post date:** [June 3, 2007, 1:25am UTC](https://www.ruby-forum.com/t/how-to-do-daily-database-maintenance-in-a-rails-way/105769/3 "2007-06-03T01:25:56Z")

</div>

I guess the Rails way is creating a rake task that depends on rails  
environment (i.e. boots the Rails runtime), doing there everything you  
need using ActiveRecord and then calling this rake task from cron

task :my\_task =\> :environment

# Do whatever you need

end

calling it:

rake my\_task

Cheers,  
Yuri

On 6/2/07, newbie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How can I do it in Rails?
> 
> Thanks!
> 
> >

–  
Best regards,  
Yuri L.

---

<div class="post-metadata">

**Author:** ![newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@newbie](https://www.ruby-forum.com/u/newbie)\
**Post date:** [June 3, 2007, 9:12am UTC](https://www.ruby-forum.com/t/how-to-do-daily-database-maintenance-in-a-rails-way/105769/4 "2007-06-03T09:12:23Z")

</div>

On Jun 2, 2007, at 6:25 PM, Yuri L. wrote:

> rake my\_task

I think that’s a fine solution. Cron is great as scheduling, and you  
can still use your Rails environment definitions (and ActiveRecord,  
if necessary).  
You may be interested in BackgrounDrb (http://  
[backgroundrb.rubyforge.org/](http://backgroundrb.rubyforge.org/)). It is designed for offloading long-  
running tasks from your Rails server, and it includes scheduling  
capabilities (similar to cron).

Thanks,

David L Altenburg  
[http://gensym.org](http://gensym.org)

---

<div class="post-metadata">

**Author:** ![newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@newbie](https://www.ruby-forum.com/u/newbie)\
**Post date:** [June 3, 2007, 1:27am UTC](https://www.ruby-forum.com/t/how-to-do-daily-database-maintenance-in-a-rails-way/105769/5 "2007-06-03T01:27:56Z")

</div>

Sorry, but isn’t it ugly, using wget to trigger some logic when you  
have script/runner and rake tasks? It’s like driving from NY to  
Washington via LA.

Cheers,  
Yuri

On 6/2/07, Bryan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Best regards,  
Yuri L.
