I\'m stuck on a simple issue here. I\'m building an application that manages a database of coupons, each of which has an expiration date. I\'m trying to build a rake task that
First off, to help debug things from rake, invoke it with the --trace
option. Your issue here isn't rake specific though.
The Offer.where('expires_on < ?', today)
is going to return a collection, and not a single instance of Offer
and there isn't a destroy
method available for the collection.
You can iterate over each expired offer and call destroy
. Something like this:
@offers = Offer.where('expires_on < ?', today)
@offers.each { |offer| offer.destroy }
You're close. You just need to use the #destroy_all method instead of #destroy
. The latter requires an id argument.
today = Date.today.to_s
Offer.where('expires_on < ?', today).destroy_all