# Clearing out filters in ActionController

**URL:** <https://www.ruby-forum.com/t/clearing-out-filters-in-actioncontroller/56709>\
**Category:** Rails\
**Created:** [April 3, 2006, 7:15am UTC](https://www.ruby-forum.com/t/clearing-out-filters-in-actioncontroller/56709 "2006-04-03T07:15:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 3, 2006, 7:15am UTC](https://www.ruby-forum.com/t/clearing-out-filters-in-actioncontroller/56709/1 "2006-04-03T07:15:34Z")

</div>

I want to run a single filter in my application.rb file, and then if a  
certain condition is true, end the action’s processing immediately.  
Right now I’m using prepend\_before\_filter to ensure that it gets run  
first, but some of my around\_filters are still being processed. Is  
there a way that I can clear out all the filters that should be run?  
Basically it’ll look something like this:

class ApplicationController \< ActionController::Base  
prepend\_before\_filter :check\_domain

def check\_domain  
unless valid\_site?  
request.filters.clear # This isn’t the right code, I just  
don’t know what is  
render :text =\> ‘This site has not been set up yet’  
end  
end  
end

valid\_site?'s implementation doesn’t matter obviously…

Also, if I use prepend\_before\_filter, will it actually run before the  
filters set up with around\_filter? Perhaps I don’t quite understand  
how the filters work exactly. But basically before anything happens,  
I want to check for one condition, and then stop processing if that  
condition exists.

Thanks,  
Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 3, 2006, 7:45am UTC](https://www.ruby-forum.com/t/clearing-out-filters-in-actioncontroller/56709/2 "2006-04-03T07:45:50Z")

</div>

btw, the around\_filter that I’m using is the ScopedAccess::Filter  
described in [http://habtm.com/articles/2006/02/22/nested-with\_scope](http://habtm.com/articles/2006/02/22/nested-with_scope)

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 3, 2006, 5:25pm UTC](https://www.ruby-forum.com/t/clearing-out-filters-in-actioncontroller/56709/3 "2006-04-03T17:25:17Z")

</div>

Pat-

```
If you want to halt the filter chain you just need to return false

```

in the filter. I am not certain if prepend\_before\_filter will make  
that filter the very first one or not but its worth a shot 😉

Cheers-  
-Ezra

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 3, 2006, 11:21pm UTC](https://www.ruby-forum.com/t/clearing-out-filters-in-actioncontroller/56709/4 "2006-04-03T23:21:34Z")

</div>

Hey Ezra,

I used prepend\_before\_filter to run a particular filter, and then  
return false in it, and everything works as I want it - no other  
filters were evaluated. Thanks for the tip.

In a bunch of my filters, I’m making the exact same DB query. I’d  
like to cache this some how so that I don’t make the same query 3-5  
times on every request. I can’t just stick it in a @variable though  
because then that belongs to the controller, which gets reused, right?  
So what would be the best way to do this - put it in flash?

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 4, 2006, 12:50am UTC](https://www.ruby-forum.com/t/clearing-out-filters-in-actioncontroller/56709/5 "2006-04-04T00:50:53Z")

</div>

Pat-

```
These are all filters in the controller? Then you should be able to

```

use an @var if you want. Otherwise the flash might be a good place  
for it since it will get cleared automatically.

-Ezra
