# Models, Helpers, Modules, etc

**URL:** <https://www.ruby-forum.com/t/models-helpers-modules-etc/71212>\
**Category:** Rails\
**Created:** [February 21, 2006, 11:27am UTC](https://www.ruby-forum.com/t/models-helpers-modules-etc/71212 "2006-02-21T11:27:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 21, 2006, 11:27am UTC](https://www.ruby-forum.com/t/models-helpers-modules-etc/71212/1 "2006-02-21T11:27:10Z")

</div>

Hello,  
I am learning rails and ruby and I have some questions about how  
should I go  
about doing certain things.

[1]Many of my models have some functionality which is common, and  
instead of  
duplicating it across models, I want to store it in a single file, and  
then  
import the file into the models. Should I do this using modules, and  
then store  
them in the `lib’ directory? What is the purpose of the tasks  
sub-directory in  
the lib folder?  
There is a similar repetition of functionality among the controllers, so  
is it  
proper to create a new controller class containing the duplicated  
functionality  
and then inheriting my other controllers from this class? Or again use  
modules  
to do so?

[2]How can I provide an alternative name for a column in a model.  
alias\_method :new\_column\_name, :old\_column\_name provides a new name only  
for  
the reader method, I want this to be done for the writer also.

## Thanks for your help.

Surendra S.  
[http://ssinghi.kreeti.com](http://ssinghi.kreeti.com), [http://www.kreeti.com](http://www.kreeti.com)  
Read the latest news at: [http://news.kreeti.com](http://news.kreeti.com)  
,----  
| “All animals are equal, but some animals are more equal than others.”  
| – Orwell, Animal Farm, 1945  
`----

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 21, 2006, 11:51am UTC](https://www.ruby-forum.com/t/models-helpers-modules-etc/71212/2 "2006-02-21T11:51:24Z")

</div>

unknown wrote:

> Hello,  
> I am learning rails and ruby and I have some questions about how  
> should I go  
> about doing certain things.
> 
> [1]Many of my models have some functionality which is common, and  
> instead of  
> duplicating it across models, I want to store it in a single file, and  
> then  
> import the file into the models. Should I do this using modules, and  
> then store  
> them in the `lib’ directory? What is the purpose of the tasks  
> sub-directory in  
> the lib folder?  
> There is a similar repetition of functionality among the controllers, so  
> is it  
> proper to create a new controller class containing the duplicated  
> functionality  
> and then inheriting my other controllers from this class? Or again use  
> modules  
> to do so?
> 
> [2]How can I provide an alternative name for a column in a model.  
> alias\_method :new\_column\_name, :old\_column\_name provides a new name only  
> for  
> the reader method, I want this to be done for the writer also.

You can create a basic\_model.rb then in all your other models do:  
Model \< BasicModel

With the controllers put the global stuff in application\_controller.rb,  
as all controllers inherit from this(unless explicity specified)

If you are getting errors about basic\_model.rb not being found, add this  
line:  
model :basic\_model  
to application\_controller.rb

Hope this helps,  
Joey

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 21, 2006, 12:12pm UTC](https://www.ruby-forum.com/t/models-helpers-modules-etc/71212/3 "2006-02-21T12:12:56Z")

</div>

joey\_\_ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> > import the file into the models. Should I do this using modules, and  
> > model :basic\_model  
> > to application\_controller.rb

When I tried to do something like the following, I get  
#42S02Table ‘BasicModel’ doesn’t exist: SHOW FIELDS FROM BasicModel

class BasicModel \< ActiveRecord::Base  
end

class Model \< BasicModel  
end

## Thanks.

Surendra S.  
[http://ssinghi.kreeti.com](http://ssinghi.kreeti.com), [http://www.kreeti.com](http://www.kreeti.com)  
Read the latest news at: [http://news.kreeti.com](http://news.kreeti.com)  
,----  
| “All animals are equal, but some animals are more equal than others.”  
| – Orwell, Animal Farm, 1945  
`----

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 22, 2006, 10:13am UTC](https://www.ruby-forum.com/t/models-helpers-modules-etc/71212/4 "2006-02-22T10:13:00Z")

</div>

Hello James,  
Thanks for your answers.

## Regards.

Surendra S.  
[http://ssinghi.kreeti.com](http://ssinghi.kreeti.com), [http://www.kreeti.com](http://www.kreeti.com)  
Read the latest news at: [http://news.kreeti.com](http://news.kreeti.com)  
,----  
| “All animals are equal, but some animals are more equal than others.”  
| – Orwell, Animal Farm, 1945  
`----

“James A.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 21, 2006, 12:46pm UTC](https://www.ruby-forum.com/t/models-helpers-modules-etc/71212/5 "2006-02-21T12:46:17Z")

</div>

If you use subclassing with active record, it assumes you want to use  
single table inheritance. The right way to go about what you’re  
looking for is to put the functionality into a module, and include  
that in the classes which need it. The lib directory is a fine place  
to put this module.

The tasks folder can hold a ‘.rake’ file in which you can create your  
own rake tasks.

For your last question:  
[http://wiki.rubyonrails.org/rails/pages/HowToAliasColumnNames](http://wiki.rubyonrails.org/rails/pages/HowToAliasColumnNames)

- james

On 2/21/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > duplicating it across models, I want to store it in a single file, and  
> > > If you are getting errors about basic\_model.rb not being found, add this  
> > > class Model \< BasicModel  
> > > `----
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

- J \*  
~
