Difference in writing a class file in lib and models folder


#1

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


#2

On Thu, Apr 9, 2009 at 11:26 AM, Sijo Kg
removed_email_address@domain.invalid 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 T.
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

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


#3

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 removed_email_address@domain.invalid


#4

On Thu, Apr 9, 2009 at 12:01 PM, Julian L.
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/
Learn rails: 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://www.linkedin.com/in/andrewtimberlake

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


#5

yes classes in lib reloading in dev mode


#6

Andrew T. wrote:

On Thu, Apr 9, 2009 at 11:26 AM, Sijo Kg
removed_email_address@domain.invalid 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.