Home > Mobile >  How to check status with a rake task
How to check status with a rake task

Time:10-15

i'm trying to make a rake task to run it with scheduler on heroku, but first im testing locally so i have a method where i check the status of polls like this

def check_status
    if Date.today.between?(self.start_date, self.expiration_date)
        self.poll_active = true
    else
        self.poll_active = false 
    end
end

and its working great but now i want this exact method to run it with a task.

i create my task file

namespace :change_poll_status do
    task :poll_status => :environment do 
        if Date.today.between?(Poll.start_date, Poll.expiration_date)
            Poll.poll_active = true
            puts "It works"
        else
            Poll.poll_active = false 
            puts "no"
        end
    end
end

but when i run rake change_poll_status:poll_status

nothing happens it just skip like there is nothing to run, no errors, nothing.

CodePudding user response:

The error is in this line:

if Date.today.between?(Poll.start_date, Poll.expiration_date)

You're trying to compare today's date with two class methods, start_date and expiration_date. These methods don't exist on the Poll class.

To fix this, you need to first retrieve an instance of the Poll class, and then call the methods on that instance. For example:

poll = Poll.first
if Date.today.between?(poll.start_date, poll.expiration_date)
  poll.poll_active = true
  puts "It works"
else
  poll.poll_active = false
  puts "no"
end
  • Related