Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rake任务失败:环境未加载_Ruby On Rails - Fatal编程技术网

Ruby on rails Rake任务失败:环境未加载

Ruby on rails Rake任务失败:环境未加载,ruby-on-rails,Ruby On Rails,我在lib/tasks/remention_emails.rake中有以下rake任务: namespace :tasks do task :send_reminder_emails => :environment do Registration.send_reminder_emails end end 当我运行bundle exec rake tasks:send_rements_email时,我设置了错误 雷克流产了! 需要/home/user/railsApps/n

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

namespace :tasks do
  task :send_reminder_emails => :environment do
    Registration.send_reminder_emails
  end
end
当我运行
bundle exec rake tasks:send_rements_email
时,我设置了错误

雷克流产了! 需要/home/user/railsApps/nso/app/models/registration.rb来定义注册

我可以从rails控制台运行
注册。发送提醒\u电子邮件
,工作正常。看起来可能我的环境没有加载?然而,据我所知:环境在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
类注册/^([A-Za-z0-9]+++++)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\+)*[A-Za-z0-9]+@(\w+\-++)*\w{1,63.[A-Za-Z]+$/2,i)
验证:学生id的数量
验证的长度为:学生id,:最小=>7,:最大=>7
def在线
self.registration.orientation!=无
终止
def发送取消电子邮件
生成\u令牌(:注册\u取消\u令牌)
self.registration\u取消\u在=Time.zone.now
拯救
NsoMailer.注册\取消\电子邮件(自助)。发送
终止
def self.send_提醒_电子邮件
#NsoMailer.send\u提醒\u email.deliver
注册=注册.where(方向id:orientation.where(课程日期:date.today+2))
注册。每个do | r|
NsoMailer.发送提醒邮件(r).发送
终止
终止
def生成_令牌(列)
开始
self[column]=SecureRandom.urlsafe_base64
注册时结束。存在?(列=>self[column])
终止
终止

我找到了一个似乎适合我的解决方案。我发现

我曾经

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\u remement\u emails
来运行rake任务…

你能发布app/models/registration.rb的内容吗?好的…添加了registration.rb看起来我问题的根源是应用程序没有加载,即使我正在调用:environment。嗯,看起来不错。但是,你的self中有一个语法错误。发送提醒邮件方法:你没有关闭注册。每个循环。我的输入错误…修复了它。