1

I am trying to extend a model from engine 1 with a concern from engine 2 through an app initializer, but I'm getting some weird behavior, here's what I've got:

Concern

module Engine2
  module Concerns
    module MyConcern

      extend ActiveSupport::Concern

      included do
        puts "Concern included!"
      end

      def jump
        puts 'Jumping!!!!'
      end
    end
  end
end

Initializer

require 'engine2/my_concern'

module Engine1
  class Member
    include Engine2::Concerns::MyConcern
  end
end

When I boot up the application, I see as expect the Concern included! message in the console, and the Member class can call the method jump, but as soon as I change any code in the host app I get the following error:

NoMethodError (undefined method 'jump' for #<Engine1::Member:0x007fe7533b4f10>)

and I have to reload the server, then it works fine again until I make another change in the host app, then it throws the error again, why is this happening and how can I avoid it?

Is there a better place where I should perform the class opening to include the concern instead of the initializer?

4

1 回答 1

0

所以我终于弄明白了,基本上发生的情况是,在开发模式下,每个模型都会在每次代码更改时重新加载,但初始化程序只在服务器启动时运行一次,所以一旦控制器中的代码发生更改,模型就会重新加载但是不再包括关注点,因此中断。

我通过将初始化程序的代码移动to_prepareapplication.rb.

对于那些不知道的人,to_prepare添加一个准备回调,它将在开发模式下的每个请求之前运行,或者在生产中的第一个请求之前运行。

于 2016-12-11T17:30:04.917 回答