Rails 4,如何将关注模型回调添加到链的末尾

时间:2014-04-17 19:41:17

标签: ruby-on-rails ruby-on-rails-4 callback before-save

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

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

1 个答案:

答案 0 :(得分: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