views:

268

answers:

1

My web application requires 6 different cron jobs to run to update timezone sensitive data within tables at 00:01:00 of each timezone. I want to create rake tasks for each time zone. Can I configure rake tasks to be timezone aware? For e.g., can I do that following in a rake task:

namespace :db do
    task :update_EST_records => :environment do
        Time.zone = "Eastern Time (US & Canada)"
        sql = "UPDATE QUERY GOES HERE WITH CREATED_AT BETWEEN ? AND ?"
        ActiveRecord::Base.establish_connection
        ActiveRecord::Base.connection.execute(sql, 
            Time.zone.now.beginning_of_day.utc, 
            Time.zone.now.end_of_day.utc)
    end
end
+2  A: 

Yes, you can. Because :update_EST_records depends on :environment task, then you have access to all your Rails environment.

You don't even have to call ActiveRecord::Base.establish_connection as long as you run the update query from your model class instead of raw SQL.

Model.update_all("...", ["CREATED_AT BETWEEN ? AND ?", value, value])
Simone Carletti