# Common Functions for Models

**URL:** <https://www.ruby-forum.com/t/common-functions-for-models/128420>\
**Category:** Rails\
**Created:** [February 7, 2008, 3:36pm UTC](https://www.ruby-forum.com/t/common-functions-for-models/128420 "2008-02-07T15:36:33Z")\
**Posts on this page:** 4\
**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 7, 2008, 3:36pm UTC](https://www.ruby-forum.com/t/common-functions-for-models/128420/1 "2008-02-07T15:36:33Z")

</div>

Hi Everyone,

1. Can anyone tell me where to put the common functions, which is to be  
used in more than one model & controller? Is there any way to create a  
“common\_functions.rb” model?  
(Or)  
is it a good practice to put all the common functions (functions with or  
without database activities) in Application controller? since  
Application controller is accessible anywhere.

2. How to access Params[][] & session[] variable in a model?

Please help me out in these issues.

Regards,  
VASANTH

---

<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 7, 2008, 3:55pm UTC](https://www.ruby-forum.com/t/common-functions-for-models/128420/2 "2008-02-07T15:55:49Z")

</div>

1. Library that gets put in lib/:

lib/common\_functions.rb:  
module CommonFunctions # or class  
… Your stuff here …  
end

In your model:

CommonFunctions.method, or you can include CommonFunctions

1. The controller should send to the model only the information it  
needs. Models do not know about params / session for a very good  
reason.

Jason

On Feb 7, 2008 9:36 AM, Vasanthakumar C.

---

<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 7, 2008, 3:57pm UTC](https://www.ruby-forum.com/t/common-functions-for-models/128420/3 "2008-02-07T15:57:35Z")

</div>

On 7 Feb 2008, at 14:36, Vasanthakumar C. wrote:

> Hi Everyone,
> 
> 1. Can anyone tell me where to put the common functions, which is to  
> be  
> used in more than one model & controller? Is there any way to create a  
> “common\_functions.rb” model?

You can create modules and include them where appropriate. It may be  
appropriate to put some things in application controller - eg all our  
controllers require you to be logged in, so the stuff to do with that  
is handled at the application controller

> (Or)  
> is it a good practice to put all the common functions (functions  
> with or  
> without database activities) in Application controller? since  
> Application controller is accessible anywhere.
> 
> 1. How to access Params[][] & session[] variable in a model?

pass them (or relevant subsets thereof) down as parameters

Fred

---

<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 11, 2008, 11:28am UTC](https://www.ruby-forum.com/t/common-functions-for-models/128420/4 "2008-02-11T11:28:05Z")

</div>

Thanks a lot for your solutions.

Regards,  
VASANTH
