I have an Rails app with a gallery of 6 images. I would like to automatically switch which images are displayed every 24 hours. I have the logic for selecting the images in a rake task, which returns an instance variable of @todays_paper:
namespace :images do
desc "TODO"
task next_batch: :environment do
all_newspapers = Newspaper.all
filtered_newspapers = all_newspapers.select { |newspaper| newspaper.done == false }
@todays_paper = filtered_newspapers.first
@todays_paper.done = true
@todays_paper.save
return @todays_paper
end
end
My question is, how do I pass the @todays_paper instance variable to the controller, and therefore to the view? If I call this in the controller action, even if I'm using Whenever or Sidekiq to run it every 24 hours, it will still run every time the controller action is called, I think.
I don't know whether this is the wrong approach - but I can't think of another way of scheduling this to happen than using a background job. Could I use a Newspaper class method, that could be called perhaps?