簡體   English   中英

在Heroku Scheduler中使用已安裝的Rails Engine中的模型

[英]Using models from a mounted Rails Engine in a Heroku Scheduler

我正在編寫一個rake任務,用作Heroku Scheduler來從已安裝的Rail引擎讀取和修改某些模型。 引擎已在Rails應用程序本身中安裝並運行良好(引擎本身位於獨立的命名空間中)

我創建了一個rake任務,引用了引擎中的模型,當Heroku Scheduler運行該任務時,出現了“未初始化的常量”錯誤。 它可以很好地加載應用程序模型(即我在app / models /中定義的模型)。

我很欣賞Heroku Scheduler在啟動時可能不會將安裝的引擎中的模型添加到加載路徑,但是我不確定明確告訴Scheduler加載這些模型的正確方法是什么。

我對Rails和安裝Rails引擎有基本的了解,但顯然不足以了解如何解決此問題。

如何在Heroku Scheduler運行的rake任務中包括已安裝的引擎模型和應用程序模型?

編輯添加了Rakefile

這是我的Rakefile,盡管不會透露太多。 都是針對特定應用的模型:

require 'payments/worker' # in the Rails lib/ dir

task :handle_notifications do
  # set the loggers to stdout for scheduled tasks
  ActiveRecord::Base.logger = Logger.new(STDOUT)
  Rails.logger = Logger.new(STDOUT)

  worker = Worker.new

  # Notification is the model defined in the engine
  Notification.not_handled.each do |notification|
    worker.handle! notification
  end
end

所以我從Heroku Scheduler日志記錄中獲得了一個uninitialized constant Notification

IIRC,您必須通過指定在Rake任務中明確設置Rails環境

task :handle_notifications => :environment do

我認為這很簡單

require 'your_engine_gem_name'

在您的Rakefile或需要引擎的任務中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM