I am using whenever gem to create a cron job. This cron job needs to run a helper method on my rails app at a regular interval. This helper method checks every instance of my model and decides to update it or not.
/app/helpers/auctions_helper.rb:
module AuctionsHelper
def self.checkForExpiredAuction
# method to update each auction that has expired
puts "There are currently #{Auction.count} auctions."
@auctions = Auction.all
@auctions.each do |auction|
if auction.end_time > Time.now.utc
auction.price = 1000000
auction.save
puts "just updated #{auction.product} auction"
end
end
puts "just updated any auctions that had expired"
end
end
schedule.rb:
set :output, "log/cron_log.log"
every 1.minute do
runner "AuctionsHelper.checkForExpiredAuction"
end
which creates the following cronjob:
# Begin Whenever generated tasks for: bestBay
* * * * * /bin/bash -l -c 'cd /home/bestbay && script/rails runner -e production '\''AuctionsHelper.checkForExpiredAuction'\'' >> log/cron_log.log 2>&1'
# End Whenever generated tasks for: bestBay
The problem I'm having is that the helper method can't access the table 'auctions'. From my cron_log.log:
Could not find table 'auctions' (ActiveRecord::StatementInvalid)
This doesn't seem to be a problem with the helper method itself. If I run from terminal:
rails runner AuctionsHelper.checkForExpiredAuction
I get a nice set of outputs from my puts messages. So why can't my cronjob access my model/table?