# Adding before\_filter from inculded Module

**URL:** <https://www.ruby-forum.com/t/adding-before-filter-from-inculded-module/107614>\
**Category:** Rails\
**Created:** [June 24, 2007, 3:06am UTC](https://www.ruby-forum.com/t/adding-before-filter-from-inculded-module/107614 "2007-06-24T03:06:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christoph\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@Christoph\_O](https://www.ruby-forum.com/u/Christoph_O)\
**Post date:** [June 24, 2007, 3:06am UTC](https://www.ruby-forum.com/t/adding-before-filter-from-inculded-module/107614/1 "2007-06-24T03:06:11Z")

</div>

Hi,

so I’ve got my nice pagination module that I’m mixing into  
application.rb. It all works really nice, except for my before\_filter  
definition: When I put it into the module, my app starts throwing 500-  
errors. Obviously I can’t add this from a module the way I’m calling  
it directly in application.rb or specific controllers, because the  
before\_filter class method seems to be out of scope or something. Is  
there a way to achieve this some other way?

Thanks in advance and greetings,  
Christoph

---

<div class="post-metadata">

**Author:** ![Christoph\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@Christoph\_O](https://www.ruby-forum.com/u/Christoph_O)\
**Post date:** [July 19, 2007, 12:46am UTC](https://www.ruby-forum.com/t/adding-before-filter-from-inculded-module/107614/2 "2007-07-19T00:46:51Z")

</div>

Really no ideas on how to do this?

On 24 Jun., 03:05, Christoph O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Christoph\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@Christoph\_O](https://www.ruby-forum.com/u/Christoph_O)\
**Post date:** [July 19, 2007, 7:16am UTC](https://www.ruby-forum.com/t/adding-before-filter-from-inculded-module/107614/3 "2007-07-19T07:16:13Z")

</div>

Christoph O. wrote:

> so I’ve got my nice pagination module that I’m mixing into  
> application.rb. It all works really nice, except for my before\_filter  
> definition: When I put it into the module, my app starts throwing 500-  
> errors. Obviously I can’t add this from a module the way I’m calling  
> it directly in application.rb or specific controllers, because the  
> before\_filter class method seems to be out of scope or something. Is  
> there a way to achieve this some other way?

Hard to say what’s going on w/o seeing any of your code. This is how I  
would do it:

module Foo  
def self.included(base)  
base.before\_filter :bar  
end

def bar  
# …  
end  
end

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![Christoph\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@Christoph\_O](https://www.ruby-forum.com/u/Christoph_O)\
**Post date:** [June 18, 2012, 8:34pm UTC](https://www.ruby-forum.com/t/adding-before-filter-from-inculded-module/107614/4 "2012-06-18T20:34:51Z")

</div>

You can also use ActiveRecord::Concer and put model related methods to  
included block.

For example:

require ‘active\_support/concern’

module Foo  
extend ActiveSupport::Concern

included do  
before\_filter :bar  
end  
end

* * *

Artur Małecki  
[http://seeweer.com](http://seeweer.com)
