rake aborted! ArgumentError: wrong number of arguments (2 for 0..1)

rake aborted! ArgumentError: wrong number of arguments (2 for 0..1)

我的 rails 任务如下所示:

  desc 'Delete records older than 7 days'
  task :delete_old_records => :environment do
  Post.delete_all('created_at < ?', 7.days.ago)
  end

我尝试使用以下方法执行任务:

 rake delete_old_records --trace

并导致此错误:

** Invoke delete_old_records (first_time)                                                                                                                                                                                                                             
** Invoke environment (first_time)                                                                                                                                                                                                                                    
** Execute environment                                                                                                                                                                                                                                                
** Execute delete_old_records                                                                                                                                                                                                                                         
rake aborted!                                                                                                                                                                                                                                                         
ArgumentError: wrong number of arguments (2 for 0..1)                                                                                                                                                                                                                 
/home/codio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/relation.rb:453:in `delete_all'                                                                                                                                       
/home/codio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/querying.rb:8:in `delete_all'                                                                                                                                         
/home/codio/workspace/lib/tasks/delete_old_records.rake:3:in `block in <top (required)>'                                                                                                                                                                              
/home/codio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `call'                                                                                                                                                                 
/home/codio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `block in execute'                                                                                                                                                     
/home/codio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `each'                                                                                                                                                                 
/home/codio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `execute'                                                                                                                                                              
/home/codio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'                                                                                                                                      
/home/codio/.rbenv/versions/2.1.5/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'                                                                                                                                                                                  
/home/codio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain'                                                                                                                                               
/home/codio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:165:in `invoke'                                                                                                                                                               
/home/codio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:150:in `invoke_task'                                                                                                                                                   
/home/codio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'                                                                                                                                 
/home/codio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `each'                                                                                                                                                          
/home/codio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block in top_level'                                                                                                                                            
/home/codio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:115:in `run_with_threads'                                                                                                                                              
/home/codio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:100:in `top_level'                                                                                                                                                     
/home/codio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:78:in `block in run'                                                                                                                                                   
/home/codio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:176:in `standard_exception_handling'                                                                                                                                   
/home/codio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:75:in `run'                                                                                                                                                            
/home/codio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/bin/rake:33:in `<top (required)>'                                                                                                                                                              
/home/codio/.rbenv/versions/2.1.5/bin/rake:23:in `load'                                                                                                                                                                                                               
/home/codio/.rbenv/versions/2.1.5/bin/rake:23:in `<main>'    

有谁知道我在这里做错了什么?我的模型看起来像:

class Post < ActiveRecord::Base
end

和 rake-gem 版本是 rake-10.4.2

你可以这样做:

timestamp = Time.now - 7.days
posts = Post.where('created_at < ?', timestamp)
posts.each { |post| post.destroy }

7.days.ago return 是 ActiveSupport::TimeWithZone 的 class。

Time.now - 7.days 将 return 一个时间对象,这就是你需要的。

或者你可以试一试:

posts = Post.where('created_at < ?', timestamp).destroy_all

不太确定你这边出了什么问题,但他在我的控制台中使用基本 Rails 模型为我工作。

试试这个

Post.delete_all(['created_at < ?', 7.days.ago])