Chris Allen Lane
Chris Allen Lane

Reputation: 6442

Destroying a Rails 3 object in rake?

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 will delete the expired coupons. The relevant code from the rakefile looks like this:

desc "Deletes expired offers from the database."
task :purge_expired => :environment do
    today = Date.today.to_s            
    Offer.where('expires_on < ?', today).destroy
end

That however fails with the following error message:

rake aborted!
wrong number of arguments (0 for 1)

I'm just not sure why. What arguments would be needed?

As an experiment, I found that this worked fine:

desc "Deletes expired offers from the database."
task :purge_expired => :environment do
    today = Date.today.to_s            
    puts Offer.where('expires_on < ?', today).count
end

That returned the right number of records, so I assume I'm successfully gathering up the right objects.

FWIW, I tried this too, and had no luck:

desc "Deletes expired offers from the database."
task :purge_expired => :environment do
    today = Date.today.to_s
    @offers = Offer.where('expires_on < ?', today)
    @offers.destroy
end

So I'm kind of out of ideas. What am I doing wrong here?

Thanks so much for your help. I'm pretty sure I wouldn't have a job if it weren't for Stack Overflow!

Upvotes: 1

Views: 812

Answers (2)

Rob Davis
Rob Davis

Reputation: 15802

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

Upvotes: 1

Aaron Hinni
Aaron Hinni

Reputation: 14736

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 }

Upvotes: 1

Related Questions