What Order Do Before Filters Occur In

What order do before filters occur in?

I suggest taking a look at the source code and API Docs on filters.

The default ordering should be

  1. :set_post
  2. :set_user

I think if you wanted to push :set_user to the top of the stack you could change the line in A to

prepend_before_action :set_user

Also worth pointing out, this isn't the only question on the topic; there are others here on SO.


As for your specific situation, it looks like you'll need to change A as I mentioned above in order to have @user be assigned by the time set_post in B runs.


As of 4.2.6 (probably changed in an earlier version), the ordering is now parent before child:

  1. :set_user
  2. :set_post

What order do before filters occur in?

I suggest taking a look at the source code and API Docs on filters.

The default ordering should be

  1. :set_post
  2. :set_user

I think if you wanted to push :set_user to the top of the stack you could change the line in A to

prepend_before_action :set_user

Also worth pointing out, this isn't the only question on the topic; there are others here on SO.


As for your specific situation, it looks like you'll need to change A as I mentioned above in order to have @user be assigned by the time set_post in B runs.


As of 4.2.6 (probably changed in an earlier version), the ordering is now parent before child:

  1. :set_user
  2. :set_post

order of before_filters in Rails controller and concerns

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

How can I specify the order that before_filters are executed?

If you refer http://api.rubyonrails.org/v2.3.8/classes/ActionController/Filters/ClassMethods.html, there is a subheading called "Filter chain ordering", here is the example code from that:

class ShoppingController < ActionController::Base
before_filter :verify_open_shop

class CheckoutController < ShoppingController
prepend_before_filter :ensure_items_in_cart, :ensure_items_in_stock

According to the explanation:

The filter chain for the CheckoutController is now
:ensure_items_in_cart, :ensure_items_in_stock,
:verify_open_shop.

So you can explicitly give the order of the filter chain like that.

What will be the order in which filters will be called?

In the order their mappings are defined in web.xml

If using annotations (@WebFilter) the order seems to be undefined - you still have to declare the <filter-mapping> entries in web.xml.

Servlet filters' order of execution

When the container recives a request, it first finds all the filter mappings with a <url-pattern> that matches the request URI. This becomes the first set of filters in the filter chain.Next it finds all the filter mappings with a <servlet-name> that matches the request URI. This becomes the second set of filters in the filter chain.In both the sets the filters are executed in the order in which they are declared in the Deployment descriptor(D.D.)

According to the specs

The order the container uses in building the chain of filters to be applied for a
particular request URI is as follows:

  1. First, the <url-pattern> matching filter mappings in the same order that these
    elements appear in the deployment descriptor.
  2. Next, the <servlet-name> matching filter mappings in the same order that these
    elements appear in the deployment descriptor.

In what order are filters executed in asp.net mvc

Filters run in the following order:

  1. Authorization filters
  2. Action filters
  3. Response filters
  4. Exception filters

For example, authorization filters run first and exception filters run last. Within each filter type, the Order value specifies the run order. Within each filter type and order, the Scope enumeration value specifies the order for filters. This enumeration defines the following filter scope values (in the order in which they run):

  1. First
  2. Global
  3. Controller
  4. Action
  5. Last

Extracted from MSDN



Related Topics



Leave a reply



Submit