2013-01-22 83 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 

完成。