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

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)?

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

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
[email protected]

Personal Blog -> http://peterwright.blogspot.com
Agile Development Blog -> http://exceeding-expectations.blogspot.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs