Search code examples
ruby-on-railsrubyruby-on-rails-3rake-task

Setting object expired field to true after comparing expired_date to current date


I'm writing my first rake task for my rails project and I have a very vague idea of how I will implement this.

Basically, my goal is to have an automated process (Cron+Whenever) that will run this rake task once a day.

The task will iterate through each Traveldeal instance and compare its expired_date field to the current date and if the Traveldeal is expired, it will set the expired boolean to true.

Here is the task I wrote based on some other raketask I found online.

namespace :traveldeal do
  desc "Expire Old Traveldeals"
  task :expired_traveldeals => :environment do
    Traveldeal.transaction do
      Traveldeal.all.each { |deal|
        if(deal.expired_date <= Date.today)
           deal.expired = 't'
           deal.save!
        end   
      }
    end
  end
end

My raketasks seems to run, but it looks like my expired field is not getting set to 't'.

Any suggestions?

Thanks.


Solution

  • You will need to reference the namespace

    rake traveldeal:expired_traveldeals