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
:set_post
: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:
:set_user
: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
:set_post
: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:
:set_user
: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:
- First, the
<url-pattern>
matching filter mappings in the same order that these
elements appear in the deployment descriptor.- 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:
- Authorization filters
- Action filters
- Response filters
- 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):
- First
- Global
- Controller
- Action
- Last
Extracted from MSDN
Related Topics
Removing or Overriding an Activerecord Validation Added by a Superclass or Mixin
Creating a Setter Method That Takes Extra Arguments in Ruby
Version Sort (With Alphas, Betas, etc.) in Ruby
Converting a Unique Seed String into a Random, Yet Deterministic, Float Value in Ruby
Rails Cannot Load Such File -- MySQL2/Mysql2 (Loaderror)
Wrapping Text into Lines at Word Boundaries
Keyword Arguments Unpacking (Splat) in Ruby
Encoding::Undefinedconversionerror: "\Xe4" from Ascii-8Bit to Utf-8
Working with Decimals in Ruby on Rails 3
Rbenv Install Ruby Build Failed
How to Find Current Abstract Route in Rails Middware
Use Ruby Array for a JavaScript Array in Erb. Escaping Quotes
Ruby on Rails Source Code Security/Obfuscation
Scanning for Unicode Numbers in a String with \D
-': Nil Can't Be Coerced into Fixnum (Typeerror)