# Access global function in Model

**URL:** <https://www.ruby-forum.com/t/access-global-function-in-model/130219>\
**Category:** Rails\
**Created:** [February 26, 2008, 2:19pm UTC](https://www.ruby-forum.com/t/access-global-function-in-model/130219 "2008-02-26T14:19:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vasan\_cec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/258eb7/32.png) [@vasan\_cec](https://www.ruby-forum.com/u/vasan_cec)\
**Post date:** [February 26, 2008, 2:19pm UTC](https://www.ruby-forum.com/t/access-global-function-in-model/130219/1 "2008-02-26T14:19:47Z")

</div>

Hi Everyone,  
Can anyone pls tell me how to access a Common Function in  
“application.rb” controller in a Model.  
For example, i am having a function to find the Factorial of a number.  
Where should i place this function, so that i can access through out the  
application (in all controllers and models)? For now i have placed in  
“application.rb” controller.

---

<div class="post-metadata">

**Author:** ![vasan\_cec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/258eb7/32.png) [@vasan\_cec](https://www.ruby-forum.com/u/vasan_cec)\
**Post date:** [February 26, 2008, 3:31pm UTC](https://www.ruby-forum.com/t/access-global-function-in-model/130219/2 "2008-02-26T15:31:51Z")

</div>

If you need it in both controllers AND models then you probably want  
to wrap it in a plain Ruby class and include it in your models.

For example…

class MathLib  
def self.factorial(n)  
…  
end  
end

Then you can call MathLib.factorial n anywhere in your code.

If this is code that you’d like to carry from project to project you  
might also consider making this a module, rather than a class, so that  
you can include the module in the classes that need to make use of the  
functionality.

On Feb 26, 8:19 am, Vasanthakumar C. \<rails-mailing-l…@andreas-
