Domanda

I'm writing a rake task to use as a Heroku Scheduler to read and modify some models from a mounted Rail engine. The engine is mounted and operating fine within the Rails app itself (the engine itself is in an isolated namespace)

I've created a rake task that references a model from the engine, and when Heroku Scheduler runs the task, I get an 'uninitialized constant' error. It loads the app models (i.e. models that I have defined in app/models/) fine.

I appreciate that the Heroku Scheduler might not add models from mounted engines to the load path when it starts up, but I'm not sure what the correct way for me to explicitly tell the Scheduler to load these models is.

I have a basic understanding of Rails and mounting Rails engines, but obviously not enough to understand how to solve this problem.

How can I include the mounted engine models along with the app models in a rake task run by Heroku Scheduler?

EDIT added Rakefile

Here is my Rakefile, although it isn't going to reveal too much. It's all just application-specific models:

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

So I get an uninitialized constant Notification from the Heroku Scheduler logging

È stato utile?

Soluzione

IIRC, you have to explicitely set up your Rails environment in Rake tasks by specifying

task :handle_notifications => :environment do

Altri suggerimenti

I think it is a simple as doing a

require 'your_engine_gem_name'

in your Rakefile or task that requires the Engine

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top