Forum: Ruby on Rails Model and Controller relationship

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.
3d333b0012928f3dd5a6861cb09ad683?d=identicon&s=25 Kris (Guest)
on 2006-03-31 15:31
My question is about where certain kinds of logic sit in the Model and
Controller objects or more specifically the relationship between Model
and Controllers.

Does an Model class contain a Controller class, the otherway round, or
are they two seperate classes that talk?


1. Should the Models be treated as the primary objects within a program
and the matching controller is simply a way of controlling access to the
class via the web.

2. The Controller classes are the primary object and simply use an
ActiveRecord class to store data.

3. Other?
085541f9546d0505433183b5f95bbf62?d=identicon&s=25 Ryan Bates (Guest)
on 2006-03-31 19:39
(Received via mailing list)
> My question is about where certain kinds of logic sit in the Model and
> Controller objects or more specifically the relationship between Model
> and Controllers.
>
> Does an Model class contain a Controller class, the otherway round, or
> are they two seperate classes that talk?

I'm fairly new to RoR, but this is my understanding of how it works
(someone please correct me if I'm wrong):

The model should be fairly self-contained, rarely communicating to
external objects. This also means that it should not communicate
directly with the controller nor should it even know one exists.
Often the model parallels a table in a database: the class being the
table, the objects (instances of the class) being rows in that table,
and the object's attributes being columns in the table.

To put it short, the model only messes with it's own stuff and no one
else's. The primary exception to this is if the model is related to
other models (such as with has_many) then it can communicate with the
related models too.

The controller, on the other hand, often sends messages to many
different models - retrieving the model objects to prepare for
displaying them in the view. However, be on the lookout for code in
the controller that just messes with the model. This should be
refactored (moved) into the model so the controller can be simpler
and other controllers can use that code.

Hope that made sense and answered your question.

Ryan
3d333b0012928f3dd5a6861cb09ad683?d=identicon&s=25 kris (Guest)
on 2006-04-01 11:00
Yeah that makes sense and thanks for the answer, but I was kinda
wondering _within rails_ what the controller-model relationship is
object wise. Does the controller spawm a model... In fact I've not seen
any info on really how rails works internally, I guess most people are
not really intrested and just wanna use rails :)

I'm not thinking of rails, as how do I use it, but what is it... how
does it work.

I don't think of the models as really having a relationship to
ActiveRecord, even though many do. More that models could be decended
from AR if they need to be persistant by storing their state in a
database.

Models must have an independant purpose seperate from AR?
3d333b0012928f3dd5a6861cb09ad683?d=identicon&s=25 Kris (Guest)
on 2006-04-03 11:12
Models are classes which model concepts such as Person or Invoice.
Controllers are classes which control access to models.

Are there any documents on the actual internals of Rails or is it a case
of trying to trace the source code, if so where would a good starting
point be, Dispaches?
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-04-03 11:22
(Received via mailing list)
Kris wrote:
> Models are classes which model concepts such as Person or Invoice.
> Controllers are classes which control access to models.
>
> Are there any documents on the actual internals of Rails or is it a case
> of trying to trace the source code, if so where would a good starting
> point be, Dispaches?

It depends on what you're trying to find out...  What's the question,
exactly?
88617b8c1550757f36cb71304b3d7d43?d=identicon&s=25 John (Guest)
on 2006-04-03 12:06
Alex Young wrote:
> Kris wrote:
>> Models are classes which model concepts such as Person or Invoice.
>> Controllers are classes which control access to models.
>>
>> Are there any documents on the actual internals of Rails or is it a case
>> of trying to trace the source code, if so where would a good starting
>> point be, Dispaches?
>
> It depends on what you're trying to find out...  What's the question,
> exactly?


This might help...
http://api.rubyonrails.org/

:)
88617b8c1550757f36cb71304b3d7d43?d=identicon&s=25 John (Guest)
on 2006-04-03 12:09
John wrote:
> Alex Young wrote:
>> Kris wrote:
>>> Models are classes which model concepts such as Person or Invoice.
>>> Controllers are classes which control access to models.
>>>
>>> Are there any documents on the actual internals of Rails or is it a case
>>> of trying to trace the source code, if so where would a good starting
>>> point be, Dispaches?
>>
>> It depends on what you're trying to find out...  What's the question,
>> exactly?
>
>
> This might help...
> http://api.rubyonrails.org/
>
> :)
Try this too..
Coz I am a newbie myself...I keep searching for this stuff...

http://glu.ttono.us/articles/2006/03/21/rails-for-designers

Enjoy!
3d333b0012928f3dd5a6861cb09ad683?d=identicon&s=25 Kris (Guest)
on 2006-04-04 10:14
"It depends on what you're trying to find out...  What's the question,
exactly?"

Its at the top ;) Well I answered my own question about what models and
controllers are, but not how they relate. I wanted to get a grasp of how
rails works not how you use rails.

I thinking about how, as objects/classes within the MCV restraints, the
controller and model relate. Is the model a class within the controller
class for example or are they seperate classes and the controller
guesses the model class name.

The API does not seem to give this kind of information just how to use,
not how it works...

I guess its a case of digging though the rails source !
3d333b0012928f3dd5a6861cb09ad683?d=identicon&s=25 kris (Guest)
on 2006-04-04 15:50
Poor spelling.. it should read:

> I thinking about how, as objects/classes within the Model-Controller-View pattern, the 
controller and model relate in practice. Is the model a class within the controller class 
for example or are they seperate classes and the controller 'guesses' the model class 
name.



Kris wrote:
> "It depends on what you're trying to find out...  What's the question,
> exactly?"
>
> Its at the top ;) Well I answered my own question about what models and
> controllers are, but not how they relate. I wanted to get a grasp of how
> rails works not how you use rails.
>
> I thinking about how, as objects/classes within the MCV restraints, the
> controller and model relate. Is the model a class within the controller
> class for example or are they seperate classes and the controller
> guesses the model class name.
>
> The API does not seem to give this kind of information just how to use,
> not how it works...
>
> I guess its a case of digging though the rails source !
085541f9546d0505433183b5f95bbf62?d=identicon&s=25 Ryan Bates (Guest)
on 2006-04-05 00:40
(Received via mailing list)
> I thinking about how, as objects/classes within the Model-
> Controller-View pattern, the controller and model relate in
> practice. Is the model a class within the controller class for
> example or are they seperate classes and the controller 'guesses'
> the model class name.

Rails does a lot name-matching, but that is mostly matching the table
to the model. I don't think the model and controller have any special
relationship under-the-hood. They are simply two separate classes.
The controller can call model methods and store model objects as
instance variables just like any other class/object can. Also, the
controller has equal access to all models, it is not tied to specific
models so there's no reason to do any name-guessing. Is there some
code that you have seen which looks like the controller and model
have a special relationship? Are you referring to scaffolding by chance?

Ryan
D5145c421cd25af6fa577c15219add90?d=identicon&s=25 unknown (Guest)
on 2006-04-05 01:50
(Received via mailing list)
Well, if you're using scaffolding, and you only provide one argument
to your script/generate scaffold, then the model and controller will
have the same name. So scaffolding gives them a relationship, but
after you do that, you could quite easily rename your controller, and
the relevant view stuff, and it'd still work fine.
3d333b0012928f3dd5a6861cb09ad683?d=identicon&s=25 kris (Guest)
on 2006-04-11 09:55
Thanks that is the kind of info I'm after. There is no code I have
looked at that made me ask the question I just wanted to know more about
Rails internals and how it impliments MVC.

I have looked though the Rails core code but it would be nice to have a
guide. There are a lot of files/includes and you dont have the best of
ideas at first glance what calls what etc. That said there are a lot of
comments which is great.



Ryan Bates wrote:
>> I thinking about how, as objects/classes within the Model-
>> Controller-View pattern, the controller and model relate in
>> practice. Is the model a class within the controller class for
>> example or are they seperate classes and the controller 'guesses'
>> the model class name.
>
> Rails does a lot name-matching, but that is mostly matching the table
> to the model. I don't think the model and controller have any special
> relationship under-the-hood. They are simply two separate classes.
> The controller can call model methods and store model objects as
> instance variables just like any other class/object can. Also, the
> controller has equal access to all models, it is not tied to specific
> models so there's no reason to do any name-guessing. Is there some
> code that you have seen which looks like the controller and model
> have a special relationship? Are you referring to scaffolding by chance?
>
> Ryan
This topic is locked and can not be replied to.