Search code examples
ruby-on-railsruby-on-rails-4actioncontrollerbefore-filteractivesupport-concern

order of before_filters in Rails controller and concerns


I have a Rails concern defined as follows:

module MyConcern
  extend ActiveSupport::Concern

  included do
    before_filter :filter_inside_concern
  end

  def filter_inside_concern
    # ...
  end
end

and I have a before_filter also on the controller layer:

class MyController < ApplicationController
  before_filter :filter_inside_controller
end

If I include MyConcern inside MyController, does the order in which the before filters are called dependent on how the code is arranged? For example, if we have

class MyController < ApplicationController
  include MyConcern

  before_filter :filter_inside_controller
end

Does filter_inside_concern gets called before filter_inside_controller (or vice versa)?

Thank you!


Solution

  • I have recreated your situation and find out sequence of execution depends on sequence in which you write both filters.

    if you write

      include MyConcern
      before_filter :filter_inside_controller
    

    concern filter will execute first

    or if you write filters in this sequence

      before_filter :filter_inside_controller
      include MyConcern
    

    controller filter will execute first