# Model equivalent for controllers/application.rb

**URL:** <https://www.ruby-forum.com/t/model-equivalent-for-controllers-application-rb/130728>\
**Category:** Rails\
**Created:** [March 2, 2008, 6:25pm UTC](https://www.ruby-forum.com/t/model-equivalent-for-controllers-application-rb/130728 "2008-03-02T18:25:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [March 2, 2008, 6:25pm UTC](https://www.ruby-forum.com/t/model-equivalent-for-controllers-application-rb/130728/1 "2008-03-02T18:25:51Z")

</div>

Hi All

I have a couple of functions in my modules which are identical. I would  
like to move them into a separate file and have them available in all  
modules. For example, the controllers already have one, application.rb.  
Do modules have something similar ? If not, what would be the best way  
to do this ?

Thnx a lot  
LuCa

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [March 2, 2008, 8:25pm UTC](https://www.ruby-forum.com/t/model-equivalent-for-controllers-application-rb/130728/2 "2008-03-02T20:25:24Z")

</div>

On Mar 2, 2008, at 9:25 AM, Luca S. wrote:

> ## Thnx a lot LuCa

Create a new file … call it lib/model\_helpers.rb or something like  
that:

module ModelHelpers  
def identical\_method  
# do some stuff  
end  
end

Then in your model class:

class MyModel  
include ModelHelpers

# more stuff

end

This should allow you to get the methods where you need them.

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [March 3, 2008, 5:54pm UTC](https://www.ruby-forum.com/t/model-equivalent-for-controllers-application-rb/130728/3 "2008-03-03T17:54:55Z")

</div>

Luca – modules can include other modules so you can refactor in the  
same way you already have.

On Mar 2, 12:25 pm, Luca S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [March 4, 2008, 8:26pm UTC](https://www.ruby-forum.com/t/model-equivalent-for-controllers-application-rb/130728/4 "2008-03-04T20:26:14Z")

</div>

thanks a lot, I have it working now!!  
LuCa

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [March 8, 2008, 1:14pm UTC](https://www.ruby-forum.com/t/model-equivalent-for-controllers-application-rb/130728/5 "2008-03-08T13:14:21Z")

</div>

nice solution, so I just create a helpermodule which extends  
ActiveRecords, is that correct ?

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [March 5, 2008, 1:09am UTC](https://www.ruby-forum.com/t/model-equivalent-for-controllers-application-rb/130728/6 "2008-03-05T01:09:18Z")

</div>

> class MyModel  
> include ModelHelpers
> 
> # more stuff
> 
> end
> 
> This should allow you to get the methods where you need them.

Or one step further:

class ActiveRecord::Base  
include ModelHelpers  
end

you can just open the big daddy yourself and mess with it if the helpers  
are going to be used by most of your models anyways, this will help you  
keep your individual model code dryer and give you something to talk  
about at your next party.

hth

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [March 8, 2008, 2:16pm UTC](https://www.ruby-forum.com/t/model-equivalent-for-controllers-application-rb/130728/7 "2008-03-08T14:16:55Z")

</div>

Way I would do it

# /config/initializers/record\_extender.rb

require ‘lib/extensions/active\_record\_extender’  
ActiveRecord::Base.send( :include, ActiveRecordExtender )

# /lib/extensions/active\_record\_extender.rb

module Extensions  
module ActiveRecordExtender  
def shared\_method  
# this method will be in every single model  
end  
end  
end

# /app/models/example.rb

class Example \< AR:Base  
def some\_method  
shared\_method # call to method from module  
end  
end

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [March 21, 2008, 2:51pm UTC](https://www.ruby-forum.com/t/model-equivalent-for-controllers-application-rb/130728/8 "2008-03-21T14:51:02Z")

</div>

> ```
> # this method will be in every single model
> end
> 
> ```
> 
> end  
> end
> 
> # /app/models/example.rb
> 
> class Example \< AR:Base  
> def some\_method  
> shared\_method # call to method from module  
> end  
> end

Doesn’t work for me. Mongrel chokes on startup:

./lib/extensions/active\_record\_extender.rb:1: Extensions is not a  
module (TypeError)

Copied and pasted as is from the post.
