# Filters with :only and controller inheritance

**URL:** <https://www.ruby-forum.com/t/filters-with-only-and-controller-inheritance/111355>\
**Category:** Rails\
**Created:** [August 6, 2007, 2:23pm UTC](https://www.ruby-forum.com/t/filters-with-only-and-controller-inheritance/111355 "2007-08-06T14:23:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jaime\_Cham](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/258eb7/32.png) [@Jaime\_Cham](https://www.ruby-forum.com/u/Jaime_Cham)\
**Post date:** [August 6, 2007, 2:23pm UTC](https://www.ruby-forum.com/t/filters-with-only-and-controller-inheritance/111355/1 "2007-08-06T14:23:08Z")

</div>

Not sure if this is a bug, or what the correct behaviour should be.  
Suppose that we have a parent and an inherited controller:

class TestController \< ApplicationController

def filter  
logger.debug “filter”  
end

before\_filter :filter, :only =\> [:t1]

def t1  
logger.debug “TestController t1”  
render :text =\> ‘t1’  
end

end

class TestChildController \< TestController

before\_filter :filter, :only =\> [:t3] # oops what should this do?

def t3  
logger.debug “TestChildController t3”  
render :text =\> “t3”  
end

end

The idea here is that the child controller might want to reuse the  
filter for its new ‘t3’ action.

What happens though is rather unexpected:

Processing TestChildController#t1 (for 127.0.0.1 at 2007-08-06  
20:09:13) [GET]  
Session ID: 082b6fa688bac1b0b70baa770b5754ba  
Parameters: {“action”=\>“t1”, “controller”=\>“test\_child”}  
TestController t1 \<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\< filter not called  
Completed in 0.00010 (10000 reqs/sec) | Rendering: 0.00000 (0%) | DB:  
0.00000 (0%) | 200 OK [[http://localhost/test\_child/t1](http://localhost/test_child/t1)]

Processing TestChildController#t3 (for 127.0.0.1 at 2007-08-06  
20:09:21) [GET]  
Session ID: 082b6fa688bac1b0b70baa770b5754ba  
Parameters: {“action”=\>“t3”, “controller”=\>“test\_child”}  
filter  
filter \<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\< why called twice?  
TestChildController t3  
Completed in 0.00010 (10000 reqs/sec) | Rendering: 0.00000 (0%) | DB:  
0.00000 (0%) | 200 OK [[http://localhost/test\_child/t3](http://localhost/test_child/t3)]

One would expect that either the new :only overrides the previous one  
and only t3 invokes the filter (sensical but not convenient) or the  
new :only just adds the filter to t3 and keeps the filter on t1. Or is  
using :only here inherently bad?

Thanks!

Jaime Cham
