Rake任务失败:环境未加载

时间:2014-01-29 16:55:11

标签: ruby-on-rails

我在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,它运行正常。看起来我的环境可能没有加载?但是,根据我的理解,环境在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

1 个答案:

答案 0 :(得分:0)

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

Simple way of turning off observers during rake task?

我用过......

Rails.configuration.active_record.observers = []

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

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

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