2013-01-22 47 views
4

我在LIB /模型/ alert_import”文件alert_import,我想在我的任务某物使用这样的:需要LIB在rake任务

task :send_automate_alerts => :environment do 
# STDERR.puts "Path is #{$:}" 
    Rake.application.rake_require '../../lib/models/alert_import' 
    ai = AlertImport::Alert.new(2) 
    ai.send_email_with_notifcations 
end 

在这段代码中,我得到错误:

找不到../../lib/models/alert_import在AlertImport

我:

module AlertImport 

    class Alert 

    def initialize(number_days) 
     @number_days = number_days 
    end 

    def get_all_alerts 
     alerts = { } 
     Organization.automate_import.each do |o| 
     last_import = o.import_histories.where(import_type: "automate").last 
     last_successful_import = ImportHistory.last_automate_successful_import(o) 
     if last_import 
      if last_import.created_at + @number_days.days >= Time.now 
      alerts[o.id] ="Error during last automate import Last successful import was #{ last_successful_import ? last_successful_import.created_at : "never"}" if last_import.status == "failure" 
      alerts[o.id] ="Error during last automate import - status pending Last successful import was #{ last_successful_import ? last_successful_import.created_at : "never"}" if last_import.status == "pending" 
      else 
      alerts[o.id] = "There were no new files uploaded within #{@number_days} days" 
      end 
     else 
      alerts[o.id] = "The import was never triggered at all" 
     end 
     end 
     alerts 
    end 

    def send_email_with_notifcations 
     alerts =get_all_alerts 
     unless alerts.empty? 
     AlertMailer.email_notifications(alerts).deliver 
     end 
    end 

    end 

end 

正确的解决办法是:

desc "Send alerts about automate imports" 

task :send_automate_alerts => :environment do 
    require "#{Rails.root}/lib/models/alert_import" 
    ai = AlertImport::Alert.new(2) 
    ai.send_email_with_notifcations 
end 
+0

您的路径错误。 –

回答

1

你的道路是错误的,你可以尝试:

task :send_automate_alerts => :environment do 
# STDERR.puts "Path is #{$:}" 
    Rake.application.rake_require "#{Rails.root}/lib/models/alert_import" 
    ai = AlertImport::Alert.new(2) 
    ai.send_email_with_notifcations 
end 

商祺!

+1

它没有帮助 – user1013369

+0

工程需要“#{Rails.root}/lib/models/alert_import”(它没有错误),但现在看不到模型:未初始化的常量AlertImport :: Alert :: Organization(组织在数据库中) – user1013369

+0

我认为你的路径错误是固定的。您必须提出其他问题并接受此回复,如果您已修复路径错误。谢谢! – hyperrjas

3

最大的可能是你的路径不对,你可以做如下

task :send_automate_alerts => :environment do 
# STDERR.puts "Path is #{$:}" 
    Rake.application.rake_require "#{Rails.root}/lib/models/alert_import" 
    ai = AlertImport::Alert.new(2) 
    ai.send_email_with_notifcations 
end 

"#{Rails.root}"这会给你的项目

+1

它没有帮助我有相关的路径,但同样的问题 – user1013369

5

在Rails 3.x,我通过使用require首先导入文件,然后将模块包含到命名空间中,从而取得了成功。下面是它会怎样看:

require 'models/alert_import' 

namespace :alerts 

    include AlertImport 

    desc 'Send alerts about automate imports' 
    task send_automate_alerts: :environment do 
    ai = AlertImport::Alert.new(2) 
    ai.send_email_with_notifcations 
    end 

end 
2

我尝试了几个选项,最显着的努力rake require,但它看起来像文档rake_require不正确。它具体将不包括不.rake

,我做到了“从头开始”端,以便在最后的文件 - 这样的事情: ```

namespace :my_namespace do 
    task :my_task do 
    require File.join(Rails.root, 'app', 'services', 'my_module.rb') 

    class Wrapper 
     include MyModule 
    end 
    Wrapper.new.the_method_I_need(args) 
    end 
end 

完成。