Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify the order that before_filters are executed?

Does rails make any guarantees about the order that before filters get executed with either of the following usages:

before_filter [:fn1, :fn2] 

or

before_filter :fn1 before_filter :fn2 

I'd appreciate any help.

like image 754
James Avatar asked Apr 19 '11 04:04

James


Video Answer


1 Answers

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.

like image 70
Johnny Woo Avatar answered Oct 27 '22 00:10

Johnny Woo