繁体   English   中英

耙任务失败:环境未加载

[英]Rake task failing: Environment not loading

我在lib / tasks / reminder_emails.rake中具有以下rake任务:

namespace :tasks do
  task :send_reminder_emails => :environment do
    Registration.send_reminder_emails
  end
end

当我运行bundle exec rake tasks:send_reminder_emails我设置了错误...

耙子流产了! 期望/home/user/railsApps/nso/app/models/registration.rb定义注册

我可以从Rails控制台运行Registration.send_reminder_emails ,它可以正常运行。 看起来我的环境未加载? 但是,据我了解,这就是:environment在rake任务中的作用。

有想法吗?

编辑:app / models / registration.rb的内容

class Registration < ActiveRecord::Base
  belongs_to :orientation
  attr_accessible :first_name, :last_name, :email, :student_id, :phone, :orientation_id, :checked_in

  validates_presence_of :first_name
  validates_presence_of :last_name
  validates_presence_of :phone
  validates_presence_of :email
  validates_format_of :email, :with => /^(|(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6})$/i
  validates_numericality_of :student_id
  validates_length_of :student_id, :minimum => 7, :maximum => 7

  def online
    self.registration.orientation != nil
  end

  def send_cancellation_email
    generate_token(:registration_cancellation_token)
    self.registration_cancelled_at = Time.zone.now
    save!
    NsoMailer.registration_cancellation_email(self).deliver
  end

  def self.send_reminder_emails
      #NsoMailer.send_reminder_emails.deliver
    registrations = Registration.where(orientation_id: Orientation.where(class_date: Date.today + 2))
    registrations.each do |r|
      NsoMailer.send_reminder_emails(r).deliver
    end
  end

  def generate_token(column)
    begin
      self[column] = SecureRandom.urlsafe_base64
    end while Registration.exists?(column => self[column])
  end
end

我找到了似乎对我有用的解决方案。 我发现...

在rake任务期间关闭观察者的简单方法?

我用了...

Rails.configuration.active_record.observers = []

...在我的耙任务中禁用观察者。 所以我现在完成的耙任务看起来像是...

task :send_reminder_emails => :environment do
  Rails.configuration.active_record.observers = []
  Registration.send_reminder_emails
end

...我只是运行bundle exec rake send_reminder_emails来运行rake任务...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM