# Controller & Multiple Models (And a self-referencing model!)

**URL:** <https://www.ruby-forum.com/t/controller-multiple-models-and-a-self-referencing-model/69142>\
**Category:** Rails\
**Created:** [September 18, 2006, 11:03pm UTC](https://www.ruby-forum.com/t/controller-multiple-models-and-a-self-referencing-model/69142 "2006-09-18T23:03:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![atraeyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b19c9b/32.png) [@atraeyu](https://www.ruby-forum.com/u/atraeyu)\
**Post date:** [September 18, 2006, 11:03pm UTC](https://www.ruby-forum.com/t/controller-multiple-models-and-a-self-referencing-model/69142/1 "2006-09-18T23:03:51Z")

</div>

I’m new to rails, trying to get a basic understanding and I’ve  
encountered two concepts I can’t wrap my head around and can’t find  
answers for elsewhere. I’ve been looking for answers for hours and  
can’t seem to get anywhere. I apologize in advance for the really basic  
questions.

I have a database with three tables:  
users  
items  
item\_types

I’ve generated three models:  
User  
Item  
ItemType

I’ve generated one controller so far:  
Admin

Now, if I can generate a scaffold for my Admin controller using any  
single model, but how about all three? Typically the directory  
structure is:  
application/controller/action

But what if I want:  
application/controller/subapp/action

Basically, I’d like to generate scaffolds for:  
/admin/user/list (edit/new/delete/show, etc)  
/admin/item/list (edit/new/delete/show, etc)  
/admin/itemtype/list (edit/new/delete/show, etc)

Is this possible? If not, what is the alternative?

Second, my Item model contains an integer field “parent.” The field  
contains either 0 or a valid item\_id … so it’s self referencing. Some  
items belong to other items.

I know how to setup a has\_one or has\_many relationship using different  
models, but how do I specify that the product field points back to  
another model (if it does not contain 0)?

---

<div class="post-metadata">

**Author:** ![atraeyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b19c9b/32.png) [@atraeyu](https://www.ruby-forum.com/u/atraeyu)\
**Post date:** [September 19, 2006, 6:05pm UTC](https://www.ruby-forum.com/t/controller-multiple-models-and-a-self-referencing-model/69142/2 "2006-09-19T18:05:08Z")

</div>

Sorry to bump myself. Hoping someone may have an answer though.  
Thanks. 🙂

---

<div class="post-metadata">

**Author:** ![atraeyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b19c9b/32.png) [@atraeyu](https://www.ruby-forum.com/u/atraeyu)\
**Post date:** [September 25, 2006, 10:19pm UTC](https://www.ruby-forum.com/t/controller-multiple-models-and-a-self-referencing-model/69142/3 "2006-09-25T22:19:56Z")

</div>

Not entirely sure I have a good grasp on your question, but here goes.

First up, yes you can access any model from any controller.  
Controllers tend to span models in producing and manipulating data  
for the view.

If you’re talking about creating controllers for each model under a  
common admin entry point, you can do that

script/generate controller admin/Usercontroller  
script/generate controller admin/Itemcontroller

and so on

try it in a test project and you’ll see what I mean.

## Hope that helps?!

Peter Wright  
removed\_email\_address@domain.invalid

Personal Blog → [http://peterwright.blogspot.com](http://peterwright.blogspot.com)  
Agile Development Blog → [http://exceeding-expectations.blogspot.com](http://exceeding-expectations.blogspot.com)
