1

I am building an application in rails that allows users to post products for sale for a set duration of time. Each product has a different 'end_time' in which I want to run a method that sets that updates the product status to 'dead'.
I've been reading up on the Wheneverize gem, however as far as I can tell I would use it for doing a recurring task every minute, hour, day, etc... Any ideas on how I would go about to running a cron job that would run at each product's specific 'end_time'?

for example something along the lines of:

live_products = Product.where('status = ?', 'live')

live_products.each do |product|
 at: product.end_time do
   product.status = 'dead'
   product.save
 end
end 
Sebin
  • 1,044
  • 1
  • 8
  • 21
  • look on this link.. may help you [stackoverflow.com/questions/4602418](http://stackoverflow.com/questions/4602418/rails-whenever-gem-dynamic-values/31075237#31075237) – Mukesh May 25 '16 at 05:35

1 Answers1

0

Cronjob, every minute, checking for those that need transaction to dead.

Product.turn_dead.update_all status: :dead 



#product.rb
scope :live, -> {where status: :live}
scope :out_of_time, -> {where "end_time <= ?", Time.now}
scope :turn_dead, ->{ live.out_of_time }
Tim Kretschmer
  • 2,272
  • 1
  • 22
  • 35