簡體   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