Model and Controller relationship

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?

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

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 :slight_smile:

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?

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?

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?

John wrote:

Alex Y. 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/

:slight_smile:
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!

“It depends on what you’re trying to find out… What’s the question,
exactly?”

Its at the top :wink: 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 !

Alex Y. 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/

:slight_smile:

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 :wink: 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 !

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.

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

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 B. 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