Rails - delete_all或destroy_all不能在生产服务器上运行,但在开发服务器上运行良好

时间:2011-07-15 23:28:54

标签: ruby-on-rails

我有一个rails应用程序,我需要定期从模型中删除搜索日志。为此,我使用语法 model.destroy_all 的rake任务,它在开发服务器中正常工作。但是,生产服务器上似乎没有发生任何事情。

开发和生产服务器都使用Ubuntu Server 10.04,Rails 3.0.7,Mysql Database。你以前遇到过类似的情况吗?

以下是佣金任务代码:

task :cleansed_log => :environment do
raw_logs = Searchlog.find_by_sql("SELECT q from searchlogs")
flag = 0
pres_log, prev_log = "", ""
if raw_logs.count > 1
  raw_logs.each do |raw_log|

    if flag == 0
      prev_log = raw_log.q
      flag = 1

    else

      pres_log = raw_log.q
      if pres_log =~ /#{prev_log}/
        prev_log = pres_log

      else
        @cleansedlog = Cleansedlog.new(:keyword => prev_log)
        @cleansedlog.save

        prev_log = pres_log
      end

    end
  end
  @cleansedlog = Cleansedlog.new(:keyword => pres_log)
  @cleansedlog.save

  Searchlog.destroy_all

end

end

下面是调用rake任务的cron作业:

# Begin Whenever generated tasks for: cleansedlog
# 0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd       /var/www/prodsite//releases/20110715035538 && RAILS_ENV=production rake cleansed_log -- silent >> /work/saptcodes/cron.log 2>&1'
# End Whenever generated tasks for: cleansedlog

2 个答案:

答案 0 :(得分:0)

我不确定,但您是否将RAILS_ENV选项传递给了rake任务?

例如:

rake logs:destroy RAILS_ENV='production'

答案 1 :(得分:0)

你可以试试吗

model.delete_all

小心,因为这不会尊重回调,依赖关联并直接进入数据库。

我有时遇到过与destroy_all类似的行为,尤其是在迁移中。