# Difference in writing a class file in lib and models folder

**URL:** <https://www.ruby-forum.com/t/difference-in-writing-a-class-file-in-lib-and-models-folder/165127>\
**Category:** Rails\
**Created:** [April 9, 2009, 11:26am UTC](https://www.ruby-forum.com/t/difference-in-writing-a-class-file-in-lib-and-models-folder/165127 "2009-04-09T11:26:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [April 9, 2009, 11:26am UTC](https://www.ruby-forum.com/t/difference-in-writing-a-class-file-in-lib-and-models-folder/165127/1 "2009-04-09T11:26:54Z")

</div>

Hi  
I would like to know the difference between writing our own class  
file(or module) in lib folder and models folder (Or any other folder).  
Actually what is the use of this lib folder in rails…Cant I write the  
same in models folder (not inheriting from ActiveRecord class)

Thanks in advance  
Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [April 9, 2009, 11:38am UTC](https://www.ruby-forum.com/t/difference-in-writing-a-class-file-in-lib-and-models-folder/165127/2 "2009-04-09T11:38:00Z")

</div>

On Thu, Apr 9, 2009 at 11:26 AM, Sijo Kg  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

You could but if it’s not a model, why put it in a folder labelled  
models?  
It’s purely for code organisation, and Rails uses the folder names for  
some of it’s auto load magic.

I use a library called simple\_form which creates a non-ActiveRecord  
model for contact forms but it behaves like a model as far as Rails is  
concerned so I will put that class in the models folder.

Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [April 9, 2009, 12:02pm UTC](https://www.ruby-forum.com/t/difference-in-writing-a-class-file-in-lib-and-models-folder/165127/3 "2009-04-09T12:02:07Z")

</div>

Lib is mostly for your own classes that don’t change much. Hence, in  
dev mode Theyre not reloaded. Models are. Models can be any classes,  
inheriting from anywhere. They’re or your business objects

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

On 09/04/2009, at 7:26 PM, Sijo Kg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [April 9, 2009, 12:12pm UTC](https://www.ruby-forum.com/t/difference-in-writing-a-class-file-in-lib-and-models-folder/165127/4 "2009-04-09T12:12:43Z")

</div>

On Thu, Apr 9, 2009 at 12:01 PM, Julian L.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Lib is mostly for your own classes that don’t change much. Hence, in  
> dev mode Theyre not reloaded. Models are. Models can be any classes,  
> inheriting from anywhere. They’re or your business objects
> 
> Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
> Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

I disagree, a quick test in 2.2.2 shows classes in lib reloading in dev  
mode

Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [April 9, 2009, 12:45pm UTC](https://www.ruby-forum.com/t/difference-in-writing-a-class-file-in-lib-and-models-folder/165127/5 "2009-04-09T12:45:23Z")

</div>

yes classes in lib reloading in dev mode

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [April 11, 2009, 1:10am UTC](https://www.ruby-forum.com/t/difference-in-writing-a-class-file-in-lib-and-models-folder/165127/6 "2009-04-11T01:10:01Z")

</div>

Andrew T. wrote:

> On Thu, Apr 9, 2009 at 11:26 AM, Sijo Kg  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > >
> 
> You could but if it’s not a model, why put it in a folder labelled  
> models?

There is some room for debate here in terms of the definition of a model  
object. After all in a truly pure sense of the MVC design pattern a  
model object can be as simple as the String class all the way up to a  
class a complex as ActiveRecord. After all, what does a String object  
do? It manages the interface to a set of data, which is the array of  
characters. It abstracts and encapsulates the storage of that set of  
data. So in a pure sense it is a model object.

That’s the interesting thing about the MVC pattern. There is no  
absolutes. It all depends on the definitions and usage in particular  
contexts.

> It’s purely for code organisation, and Rails uses the folder names for  
> some of it’s auto load magic.

I completely agree with this. Applying MVC inside the context of Rails  
will make others that might want to work with you on your code happy.  
Typically the stuff in app/models are subclasses of ActiveRecord (or  
other ORM style model classes). And the lib folder is typically used for  
classes and modules that provide some shared behavior, whether they be  
“model” objects or not.
