Forum: Ruby on Rails difference in writing a class file in lib and models folder

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2009-04-09 11:26
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
5772c599ccab3081e0fffb1d54f3b6de?d=identicon&s=25 Andrew Timberlake (andrewtimberlake)
on 2009-04-09 11:38
(Received via mailing list)
On Thu, Apr 9, 2009 at 11:26 AM, Sijo Kg
<rails-mailing-list@andreas-s.net> wrote:
> Posted via 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 Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-04-09 12:02
(Received via mailing list)
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/
Learn rails: http://sensei.zenunit.com/

On 09/04/2009, at 7:26 PM, Sijo Kg <rails-mailing-list@andreas-s.net>
5772c599ccab3081e0fffb1d54f3b6de?d=identicon&s=25 Andrew Timberlake (andrewtimberlake)
on 2009-04-09 12:12
(Received via mailing list)
On Thu, Apr 9, 2009 at 12:01 PM, Julian Leviston
<julian@coretech.net.au> 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/
> Learn rails: http://sensei.zenunit.com/
>

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

Andrew Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2009-04-09 12:45
yes classes in lib reloading in dev mode
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2009-04-11 01:10
Andrew Timberlake wrote:
> On Thu, Apr 9, 2009 at 11:26 AM, Sijo Kg
> <rails-mailing-list@andreas-s.net> wrote:
>> Posted via 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.
This topic is locked and can not be replied to.