7

lib/models/alert_import' にファイル alert_import があります。タスク sth で次のように使用したいと思います。

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
4

5 に答える 5

9

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
于 2014-06-24T08:10:55.113 に答える
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}"これにより、プロジェクトの現在のパスが得られます

于 2013-01-22T10:32:42.853 に答える
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

よろしく!

于 2013-01-22T10:15:16.963 に答える
0

そこをチェックしてくださいhttp://rake.rubyforge.org/classes/Rake/Application.html#M000099 正しいパスを定義してください

于 2013-01-22T11:16:21.460 に答える