1
class Model1 < ActiveRecord::Base
  include UploadLoginc

  before_save do
    p "before model"
  end
end

module UploadLoginc
  extend ActiveSupport::Concern

  included do
    before_save do
      p "before module"
    end
  end
end

如何将before_saveUploadLoginc 中的回调添加到before_save回调链的末尾?

4

1 回答 1

0

我认为您不能,但是您可能对稍后的回调有另一个关注,并在模型回调之后包含关注,以便它们以正确的顺序注册。

module UploadLoginc_before
  extend ActiveSupport::Concern

  included do
    before_save :do_something_first
  end
end

module UploadLoginc_after
  extend ActiveSupport::Concern

  included do
    before_save :finally_do_this
  end
end

class Model1 < ActiveRecord::Base
  include UploadLoginc_before

  before_save :inbetween_do_this

  include UploadLoginc_after
end
于 2017-10-11T10:56:10.307 回答