0

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.

Huy
  • 10,806
  • 13
  • 55
  • 99

1 Answers1

0

You will need to reference the namespace

rake traveldeal:expired_traveldeals
Mark Swardstrom
  • 17,217
  • 6
  • 62
  • 70
  • Got my code to work. Turns out that the t in the following line should be in single quote instead of double. `deal.expired = 't'`. I'll give you the checkmark anyway. Thanks. – Huy May 14 '12 at 23:30
  • 1
    You know - I was going to ask about the boolean value but stopped myself. I assumed you were storing a string and didn't want to open up that issue. If it's a boolean, you can simplify the code a bit. Something like Traveldeal.where(deal.expired_date <= Date.today).each { |deal| deal.update_attribute(:expired, true) } – Mark Swardstrom May 15 '12 at 19:00