# Where do I define a method so it's available to all models?

**URL:** <https://www.ruby-forum.com/t/where-do-i-define-a-method-so-its-available-to-all-models/119766>\
**Category:** Rails\
**Created:** [November 10, 2007, 7:25am UTC](https://www.ruby-forum.com/t/where-do-i-define-a-method-so-its-available-to-all-models/119766 "2007-11-10T07:25:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [November 10, 2007, 7:25am UTC](https://www.ruby-forum.com/t/where-do-i-define-a-method-so-its-available-to-all-models/119766/1 "2007-11-10T07:25:23Z")

</div>

I’m trying to define a method that will be available to all Models. I  
originally put it in the ApplicationHelper file, but I now understand  
that this is only available to Views. Where do I put a common model  
method then?

PS: If it makes a difference, the method needs to be able to reference  
the calling model, i.e. passing the model instance as an argument

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [November 10, 2007, 11:21am UTC](https://www.ruby-forum.com/t/where-do-i-define-a-method-so-its-available-to-all-models/119766/2 "2007-11-10T11:21:24Z")

</div>

you can create a module mix you application\_controller

On 11ÔÂ10ÈÕ, ÏÂÎç2Ê±25·Ö, Chris B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [November 10, 2007, 11:21am UTC](https://www.ruby-forum.com/t/where-do-i-define-a-method-so-its-available-to-all-models/119766/3 "2007-11-10T11:21:59Z")

</div>

type error, must mix you model

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [November 10, 2007, 3:05pm UTC](https://www.ruby-forum.com/t/where-do-i-define-a-method-so-its-available-to-all-models/119766/4 "2007-11-10T15:05:17Z")

</div>

Raecoo C. wrote:

> you can create a module mix you application\_controller  
> type error, must mix you model
> 
> On 11ï¿½ï¿½10ï¿½ï¿½, ï¿½ï¿½ï¿½ï¿½2Ê±25ï¿½ï¿½, Chris B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

I’m sorry, I don’t understand. Do you mean that I can add it to my  
Application controller? According to the comments in that file, anything  
added will be available to all controllers, but it doesn’t mention  
models. I need it to be available to models, including when run from the  
console.

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [November 10, 2007, 4:24pm UTC](https://www.ruby-forum.com/t/where-do-i-define-a-method-so-its-available-to-all-models/119766/5 "2007-11-10T16:24:02Z")

</div>

WadeWinningham wrote:

> You can write an extension to put in the lib/ directory and include it  
> in all of your models…
> 
> module MyExtension  
> def added\_function  
> “stuff”  
> end  
> end

Ah, yes, OK. I figured it was something like that, but I just didn’t  
know where to put it.

> * * *
> 
> class MyModel \< ActiveRecord::Base  
> include MyExtension  
> end
> 
> Or you could create a new base class that inherits from  
> ActiveRecord::Base and have your models inherit from your new base  
> class.

That makes sense too. Perhaps if I was adding more than one method. I’ll  
go the route of the lib file and just require it.

Thanks!!

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [November 11, 2007, 2:55am UTC](https://www.ruby-forum.com/t/where-do-i-define-a-method-so-its-available-to-all-models/119766/6 "2007-11-11T02:55:29Z")

</div>

WadeWinningham is right

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [November 11, 2007, 10:41pm UTC](https://www.ruby-forum.com/t/where-do-i-define-a-method-so-its-available-to-all-models/119766/7 "2007-11-11T22:41:08Z")

</div>

WadeWinningham wrote:

> Or you could create a new base class that inherits from  
> ActiveRecord::Base and have your models inherit from your new base  
> class.

If I were to go this route and create a new base class, do i put it in  
it’s own file in the model folder, and then do I require that file in  
each related model, or in the application controller, or somewhere else?

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [November 12, 2007, 5:23am UTC](https://www.ruby-forum.com/t/where-do-i-define-a-method-so-its-available-to-all-models/119766/8 "2007-11-12T05:23:12Z")

</div>

Yes, you can put it in the model directory, mark it as an abstract  
model class, and make sure it won’t collide with any other model.

You don’t have to explicitly call require in other models, just  
specify it as the parent class, and Rails with automatically load the  
specific file. Make sure to follow the Class name → filename  
convention, so Rails can find it. Have Rails autoload it, so it can  
reload the file when needed in development mode.

Thanks!

Mark  
[http://www.simpleteq.com](http://www.simpleteq.com)

On Nov 12, 5:41 am, Chris B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [November 10, 2007, 4:15pm UTC](https://www.ruby-forum.com/t/where-do-i-define-a-method-so-its-available-to-all-models/119766/9 "2007-11-10T16:15:38Z")

</div>

You can write an extension to put in the lib/ directory and include it  
in all of your models…

module MyExtension  
def added\_function  
“stuff”  
end  
end

* * *

class MyModel \< ActiveRecord::Base  
include MyExtension  
end

Or you could create a new base class that inherits from  
ActiveRecord::Base and have your models inherit from your new base  
class.

On Nov 10, 12:25 am, Chris B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
