# Administrative task in Rails

**URL:** <https://www.ruby-forum.com/t/administrative-task-in-rails/182364>\
**Category:** Rails\
**Created:** [January 27, 2010, 2:49am UTC](https://www.ruby-forum.com/t/administrative-task-in-rails/182364 "2010-01-27T02:49:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [January 27, 2010, 2:49am UTC](https://www.ruby-forum.com/t/administrative-task-in-rails/182364/1 "2010-01-27T02:49:27Z")

</div>

I have an administrative task that I need to run once a day: I need  
to iterate through each user in the User table in the database, do  
some calculation involving some other models, and update each user  
record in the database.

I am thinking about just doing it at the console (env=production).  
But I don’t want to type all the statements again each time I carry  
out this task. Is there a way to put this in a script and just run  
the script?

Eventually, I’d like to schedule the task to run automatically every  
day, and have some sort of progress display when the task is run.

Thanks,

Vincent.

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [January 27, 2010, 3:07am UTC](https://www.ruby-forum.com/t/administrative-task-in-rails/182364/2 "2010-01-27T03:07:35Z")

</div>

On Tue, Jan 26, 2010 at 8:47 PM, Vincent P [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Eventually, I’d like to schedule the task to run automatically every  
> day, and have some sort of progress display when the task is run.
> 
> Thanks,
> 
> Vincent.

Vincent, you might want to consider the whenver gem and you  
can view Ryan’s screencast here:

[http://media.railscasts.com/videos/164\_cron\_in\_ruby.mov](http://media.railscasts.com/videos/164_cron_in_ruby.mov)

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [January 27, 2010, 3:09am UTC](https://www.ruby-forum.com/t/administrative-task-in-rails/182364/3 "2010-01-27T03:09:20Z")

</div>

You want to write a rake task that runs as a cron task every day.

lib/tasks/my\_task.rake

task :touch\_every\_user =\> :environment do  
User.find\_each {|user| user.touch }

# perform some other heavy calculation and save it back to the user

end

For the cron task:  
go to server, type “crontab -e”  
15 0\* \* \* /path/to/rails/root rake touch\_every\_user  
RAILS\_ENV=production

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [January 27, 2010, 3:10am UTC](https://www.ruby-forum.com/t/administrative-task-in-rails/182364/4 "2010-01-27T03:10:31Z")

</div>

On Tue, Jan 26, 2010 at 9:06 PM, Conrad T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > the script?  
> > can view Ryan’s screencast here:
> 
> [http://media.railscasts.com/videos/164\_cron\_in\_ruby.mov](http://media.railscasts.com/videos/164_cron_in_ruby.mov)
> 
> Good luck,
> 
> -Conrad

Vincent, you’ll install the gem as follows:

sudo gem install whenever

-Conrad

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [February 4, 2010, 7:56pm UTC](https://www.ruby-forum.com/t/administrative-task-in-rails/182364/5 "2010-02-04T19:56:46Z")

</div>

Thank you! This is exactly what I need.

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [January 27, 2010, 3:20am UTC](https://www.ruby-forum.com/t/administrative-task-in-rails/182364/6 "2010-01-27T03:20:28Z")

</div>

On Tue, Jan 26, 2010 at 9:07 PM, David [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> go to server, type “crontab -e”  
> 15 0\* \* \* /path/to/rails/root rake touch\_every\_user  
> RAILS\_ENV=production

The Ruby way would be to do the following:

every :day , :at =\> “6:00 pm” do  
runner “User.all.each { |user| \<do\_some\_work\> }”  
end

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [February 4, 2010, 10:35pm UTC](https://www.ruby-forum.com/t/administrative-task-in-rails/182364/7 "2010-02-04T22:35:35Z")

</div>

Hi Vincent,

To answer your initial idea, … Yes, you can create a script, using  
the same calls you would make in console, and run them using ./script/  
runner. I do this all the time for various scripting tasks for  
projects. One of the great features of rails.

You could do something like:

###### create the script to perform the updates:

$ cat ./script/update\_users.runnable  
puts “begin updating users: env=#{RAILS\_ENV}”  
begin  
User.transaction do  
User.find(:all).each do |u|  
# perform calcs, update u, …  
end  
end  
puts “done.”  
rescue Exception=\>e  
#…  
end

###### to run it against your dev env for testing:

$ ./script/runner ./script/update\_users.runnable  
begin updating users: env=development  
…

###### to run it against your prod env:

$ ./script/runner -e production ./script/update\_users.runnable  
begin updating users: env=production  
…

As for scheduling the task, you could use cron (or the windows equiv  
if running under windows) to run the script at some particular  
scheduled time/interval.

As for progress, you can always add logging calls in your script (and  
likely a command-line arg as to whether or not to show debug info to  
stdout, like the puts call in example above), either to the env’s  
existing log, or depending on your needs/wants, to a separate log file  
for the particular task at hand (ie ./log/  
development\_update\_users.log ?), that you could check to see how the  
process is going, how it went, errors encountered, etc.

Jeff

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [February 4, 2010, 8:49pm UTC](https://www.ruby-forum.com/t/administrative-task-in-rails/182364/8 "2010-02-04T20:49:38Z")

</div>

Learn by Doing wrote:

> Thank you! This is exactly what I need.

(Please quote properly when replying.)

You will also want to use ar-extensions so that you’re not doing a  
separate query for each User you update.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
