# Does the model and the controller share a 1to1 relatioship?

**URL:** <https://www.ruby-forum.com/t/does-the-model-and-the-controller-share-a-1to1-relatioship/117480>\
**Category:** Rails\
**Created:** [October 17, 2007, 10:34am UTC](https://www.ruby-forum.com/t/does-the-model-and-the-controller-share-a-1to1-relatioship/117480 "2007-10-17T10:34:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dyavorski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dyavorski](https://www.ruby-forum.com/u/dyavorski)\
**Post date:** [October 17, 2007, 10:34am UTC](https://www.ruby-forum.com/t/does-the-model-and-the-controller-share-a-1to1-relatioship/117480/1 "2007-10-17T10:34:52Z")

</div>

Hi all  
I am a very experienced programmer (and therefor can appriciate the  
rails framework as the right thing) But a totally new to Rails.  
I am trying to build a website that includes a front-end for users and  
an admin system(CMS) for the web administrator.

I have around 12 different tables that supposed to be managed by the web  
administrator. For each one of them, I created a model.

Now, I am looking to create the CMS and I would have been more then  
happy to do the following:

1)Create an “admin” controller  
2)“Scaffold” all the models under that controller

e.g.

AdminController \< ApplicationController  
Scaffold table1  
Scaffold table2  
Scaffold table3  
etc.

Well… I can’t

This lead brought me to the big question:  
Does the model and the controller share a one to one relationship?  
Thanks a lot  
Danny

---

<div class="post-metadata">

**Author:** ![dyavorski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dyavorski](https://www.ruby-forum.com/u/dyavorski)\
**Post date:** [October 17, 2007, 3:36pm UTC](https://www.ruby-forum.com/t/does-the-model-and-the-controller-share-a-1to1-relatioship/117480/2 "2007-10-17T15:36:47Z")

</div>

Hi Danny,

As far as I know if you’re using scaffolding then yes, its 1  
controller per model.

However if you are writing the controller code yourself there is no  
such limit, you can ‘access’ as many models as you want from as many  
(or as few) controllers as you want.

Generally speaking I try to keep related stuff in the same controller,  
rather than sticking to 1:1

HTH

Peter

On Oct 17, 9:34 am, Danny Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dyavorski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dyavorski](https://www.ruby-forum.com/u/dyavorski)\
**Post date:** [October 17, 2007, 3:39pm UTC](https://www.ruby-forum.com/t/does-the-model-and-the-controller-share-a-1to1-relatioship/117480/3 "2007-10-17T15:39:42Z")

</div>

The controller and model do not share a 1 to 1 relationship although  
in many apps that is what you end up with. It keeps the app simple  
and easy to understand when you go back to it later.

The think I love about rails is that most of the time when I’m  
debugging, I read the error and immediately know what the problem is.  
Recently, I took a look at a rails CMS/Shopping cart called  
Substruct. It works well but when I tried to customize it, I found I  
spent most of my time digging through controllers and models trying to  
figure out how it worked so I could change it for my needs. I could  
not find a pattern of mapping models to controllers at all and after a  
while, it reminded me of debugging php apps.

Some apps I’ve written contain controllers that are not mapped 1 to 1  
with models and that is fine because in that instance there was a good  
reason for it but even in those apps, most of the models/controllers  
are 1 to 1.

For me, the organization of controllers and models is about keeping  
the code clean and easy to understand and come back to later.

On Oct 17, 4:34 am, Danny Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dyavorski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dyavorski](https://www.ruby-forum.com/u/dyavorski)\
**Post date:** [October 17, 2007, 4:52pm UTC](https://www.ruby-forum.com/t/does-the-model-and-the-controller-share-a-1to1-relatioship/117480/4 "2007-10-17T16:52:20Z")

</div>

On 10/17/07, Danny Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Scaffold table1  
> Scaffold table2  
> Scaffold table3  
> etc.
> 
> Well… I can’t
> 
> This lead brought me to the big question:  
> Does the model and the controller share a one to one relationship?

No, there is no forced relationship between controllers and models.

The issue with multiple scaffolds in the controller is that you get  
duplicate action names generated. But you can do it with a :suffix  
option.

From the API docs for the scaffold method:

“It’s possible to use more than one scaffold in a single controller by  
specifying options[:suffix] = true. This will make scaffold :post,  
:suffix =\> true use method names like list\_post, show\_post, and  
create\_post instead of just list, show, and post. If suffix is used,  
then no index method is added.”
