# Nested Controllers/Models/Views

**URL:** <https://www.ruby-forum.com/t/nested-controllers-models-views/91732>\
**Category:** Rails\
**Created:** [April 11, 2007, 11:17pm UTC](https://www.ruby-forum.com/t/nested-controllers-models-views/91732 "2007-04-11T23:17:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nappin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nappin](https://www.ruby-forum.com/u/nappin)\
**Post date:** [April 11, 2007, 11:17pm UTC](https://www.ruby-forum.com/t/nested-controllers-models-views/91732/1 "2007-04-11T23:17:11Z")

</div>

I’m working on a project that is getting fairly large and I want to nest  
the controller, model and view files so they each have parent  
directories. I do not want the nesting to affect any of the routing or  
any of the functionality, it is purely a organizational thing.

(  
I want my files organized like this:

app/controllers/parent\_a/a\_controller  
app/controllers/parent\_b/another\_controller

app/models/parent\_a/a\_model  
app/models/parent\_b/another\_model  
)

I’ve read many conflicting things about doing this, and am confused.  
Some people say that you can simply move the files and it will just work  
because the different resources will still remain in the same namespace  
even if they are in different folders. I’ve tried this, though, and  
references to models are broken.

Is there a better way to do this? Is there something I’m not doing that  
is breaking my references?

Cheers, Ray!

---

<div class="post-metadata">

**Author:** ![nappin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nappin](https://www.ruby-forum.com/u/nappin)\
**Post date:** [April 12, 2007, 2:06am UTC](https://www.ruby-forum.com/t/nested-controllers-models-views/91732/2 "2007-04-12T02:06:02Z")

</div>

On Apr 11, 4:17 pm, Raymond O’Connor \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> I’m working on a project that is getting fairly large and I want to nest  
> the controller, model and view files so they each have parent  
> directories. I do not want the nesting to affect any of the routing or  
> any of the functionality, it is purely a organizational thing.

I’ve had pretty good luck with this. Here’s what I’ve done:

1. For models that are in subfolders, I make sure that the model is in  
a namespace that can be equivocated to a folder by the Inflector. For  
example, /app/models/extra/product.rb would look like this:

module Util

class Product \< ActiveRecord::Base  
end

end

or you could do

class Util::Product \< ActiveRecord::Base

instead.

For controllers, do the same thing - make sure the fully qualified  
name will match the folder heirarchy.

There’s one big gotcha, though - your routing will need to specify the  
full path to the controller, for example

map.connect ‘/products/:action/:id’, :controller =\> ‘subfolder/  
products’

Then everything should work.

Jeff
