# Model and controller organization

**URL:** <https://www.ruby-forum.com/t/model-and-controller-organization/53510>\
**Category:** Rails\
**Created:** [February 7, 2006, 11:50pm UTC](https://www.ruby-forum.com/t/model-and-controller-organization/53510 "2006-02-07T23:50:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 7, 2006, 11:50pm UTC](https://www.ruby-forum.com/t/model-and-controller-organization/53510/1 "2006-02-07T23:50:24Z")

</div>

Hi. I am making an application and its going to have dozens of model  
objects and maybe as many controllers.

I want to organize this stuff into logical directories … like I  
used to do in Java (using the package declaration). I am interesting  
in learning how RoR handles this seemingly common situation and also  
what are best practices using Ruby in general.

Using ./script/generate model stuff/DebitCard  
creates: app/models/stuff/debit\_card.rb (sweet!)

However, using the generate script yields confusing results:  
./script/generate scaffold stuff/Card admin/Banking debit credit  
creates: app/models/card.rb and app/controllers/admin/  
banking\_controller.rb (the controller is in a sub dir called admin,  
but Card is not in stuff)

Further, in the case of the former example, I see that the class  
declaration is prefixed with Stuff:: but no module by this name is  
created, so on first run the application pitches a NameError.

So, in general what steps should I take to create an organized model/  
controller structure?

Thank You

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 8, 2006, 1:22am UTC](https://www.ruby-forum.com/t/model-and-controller-organization/53510/2 "2006-02-08T01:22:56Z")

</div>

Peter,

IMHO you’re going about things the right way already.

The reason your models aren’t under an ‘admin’ directory is because  
your models are tied to your database, not your controllers - you  
should (probably) have one model per table (excepting HABTM-joined  
tables), while you’ll have one controller set per e.g. user role.

For the app I’m working on now, I’ve got one controller set per user  
role, so I’m generating e.g.

- script/generate controller admin/article
- script/generate controller editor/article  
to generate two distinct controllers for the user types ‘admin’ and  
‘editor’.

These two controllers expose different methods, so a user of type  
‘admin’ will see a different set of methods to a user of type 'editor.  
I’ve still got a single ‘article’ model that gets used, because the  
‘article’ model is tied to the Articles database table, which isn’t  
dependent on the type of user.

All I need to do then is to ensure that e.g. users of type ‘admin’ get  
directed to the ‘admin’ controller, and that if they try to access the  
‘editor’ controller they get rejected. I can do this fairly simply  
with a few lines of code within each controller.

Make sense?

Regards

Dave M.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 8, 2006, 1:41am UTC](https://www.ruby-forum.com/t/model-and-controller-organization/53510/3 "2006-02-08T01:41:06Z")

</div>

Yes, thanks that does make sense.

I guess my problem is that if I have 30 or 40 models, I really would  
prefer to organize them in some fashion. Some model classes extend  
others in a logical heiarchy and its a hassle to have them all  
sitting in this one models/ directory.

In order to allow your approach to work for controllers, you must  
manually have to create modules named ‘admin’ and ‘editor’, correct?

Further, I cannot see how to use the ‘model’ declaration in a  
controller to reference a model named Admin::User

I’ve tried both:

model Admin::User  
and  
model ‘admin/user’

but neither works as expected.

So what it looks like is that RoR prefers a flat organization, and in  
order to organize a set of models into a heiarchy I need to first  
create modules that match the containing directories, then instead of  
using model declaration use require and include in my controllers.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 8, 2006, 5:07am UTC](https://www.ruby-forum.com/t/model-and-controller-organization/53510/4 "2006-02-08T05:07:35Z")

</div>

I think you’re probably right - I haven’t tried creating a hierarchy  
of models in the way you’re describing.

As far as controllers go, I don’t have to manually create anything  
unusual. The top of my admin/editor controller looks like:

class Admin::EditorsController \< ApplicationController  
layout “admin”  
def index  
…

There’s no other “links” back to stuff anywhere else, and the only  
reason I’m using layout “admin” is to let me use a different CSS  
layout based on the user’s role - it’s not at all necessary except for  
aesthetic reasons.

Regards

Dave M.
