我有一个没有自己模型的Rails引擎;只是为主应用程序添加功能的控制器,视图和观察者。
我正在尝试创建一个观察者,但无法让Rails注意到它的存在 - 忽略观察者中的after_create操作和调试语句,并且在文件中插入语法错误不会在启动时或插入时引发错误观察表中的一行。
我已经尝试了here提及的所有技术,但没效果。
#/engines/loansengine/lib/loansengine/engine.rb
module Loansengine
class Engine < ::Rails::Engine
isolate_namespace Loansengine
# OBSERVERS
config.active_record.observers = ['Loansengine::TourObserver']
end
end
观察:
#/engines/loansengine/observers/loansengine/tour_observer.rb
class Loansengine::TourObserver < ActiveRecord::Observer
observe :tours
def after_create(tour)
test_observer(tour)
end
private
def test_observer(tour)
tour.agent_comments = 'pink'
tour.save
end
end
答案 0 :(得分:2)
想想我已经想到了这个:
module Loansengine
class Engine < ::Rails::Engine
isolate_namespace Loansengine
config.before_initialize do
config.active_record.observers << 'Loansengine::TourObserver'
end
end
end