# Filters order in Controllers

**URL:** <https://www.ruby-forum.com/t/filters-order-in-controllers/147106>\
**Category:** Engines\
**Created:** [August 28, 2008, 2:09pm UTC](https://www.ruby-forum.com/t/filters-order-in-controllers/147106 "2008-08-28T14:09:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Antonio\_Tapiador\_de](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@Antonio\_Tapiador\_de](https://www.ruby-forum.com/u/Antonio_Tapiador_de)\
**Post date:** [August 28, 2008, 2:09pm UTC](https://www.ruby-forum.com/t/filters-order-in-controllers/147106/1 "2008-08-28T14:09:15Z")

</div>

Hi!  
I’m moving my CMSplugin to Rails Engines, as it’s a very nice framework  
for  
certain features. Thank you!

However, I’ve a problem with filters in Controllers.

In my plugin I have the following code:

class PostsController \< ActionController::Base  
before\_filter :get\_post, :only =\> […]

[…]  
end

and in an application:

class PostsController \< ActionController::Base  
before\_filter :get\_post, :except =\> […]  
before\_filter :post\_has\_media, :only =\> […]

[…]  
end

The problem is that :get\_post should be passed before :post\_has\_media,  
and  
that doesn’t work.

If I delete the “before\_filter :get\_post” statement from the plugin,  
things  
work

What is the behaviour of filters in Controllers? Is the filter deleted  
when  
redefined in the application or there are changes in the order?

Thank you again.

---

<div class="post-metadata">

**Author:** ![Antonio\_Tapiador\_de](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@Antonio\_Tapiador\_de](https://www.ruby-forum.com/u/Antonio_Tapiador_de)\
**Post date:** [August 28, 2008, 2:13pm UTC](https://www.ruby-forum.com/t/filters-order-in-controllers/147106/2 "2008-08-28T14:13:22Z")

</div>

The order of filters will be determined by the order that the  
controller files are loaded, and this is currently plugins first, then  
application. So filters defined in the application will be invoked  
after those defined in plugins, by default.

To get around this, I believe you can call  
append\_before\_filter/prepend\_before\_filter to explicitly control where  
your filters are inserted into the filter chain.

HTH,

James

On Thu, Aug 28, 2008 at 1:08 PM, Antonio Tapiador del Dujo

---

<div class="post-metadata">

**Author:** ![Antonio\_Tapiador\_de](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@Antonio\_Tapiador\_de](https://www.ruby-forum.com/u/Antonio_Tapiador_de)\
**Post date:** [August 29, 2008, 12:28pm UTC](https://www.ruby-forum.com/t/filters-order-in-controllers/147106/3 "2008-08-29T12:28:35Z")

</div>

El Thursday 28 August 2008 14:13:04 James A.  
escribió:\> The order of filters will be determined by the order that the

> controller files are loaded, and this is currently plugins first, then  
> application. So filters defined in the application will be invoked  
> after those defined in plugins, by default.

Uhmm, that doesn’t explain the behaviour of my app.  
:-?

What happens if a filter is defined in the plugin and redefined in the  
application?

> To get around this, I believe you can call  
> append\_before\_filter/prepend\_before\_filter to explicitly control where  
> your filters are inserted into the filter chain.

Do you know how can I see the contents of the filter chain?

Thank you.

---

<div class="post-metadata">

**Author:** ![Antonio\_Tapiador\_de](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@Antonio\_Tapiador\_de](https://www.ruby-forum.com/u/Antonio_Tapiador_de)\
**Post date:** [August 29, 2008, 12:50pm UTC](https://www.ruby-forum.com/t/filters-order-in-controllers/147106/4 "2008-08-29T12:50:18Z")

</div>

On Fri, Aug 29, 2008 at 11:28 AM, Antonio Tapiador del Dujo  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> El Thursday 28 August 2008 14:13:04 James A. escribió:
> 
> > The order of filters will be determined by the order that the  
> > controller files are loaded, and this is currently plugins first, then  
> > application. So filters defined in the application will be invoked  
> > after those defined in plugins, by default.
> 
> Uhmm, that doesn’t explain the behaviour of my app.  
> :-?
> 
> What happens if a filter is defined in the plugin and redefined in the  
> application?

Ah - good question. Sorry if I missed it in my earlier response!

I believe the behaviour would be the same as if you added the filter  
twice in the same controller - it will get called twice. My advice  
would be not define the filter in both your application AND the plugin  
controllers - only define it in one.

> Do you know how can I see the contents of the filter chain?

I’m not sure about this off-hand, but the Rails docs should be helpful.
