Forum: Ruby on Rails model and controller organization

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2ff47d48fde7f8f2d1e25ab7ccc87d00?d=identicon&s=25 Peter Brown (Guest)
on 2006-02-07 23:50
(Received via mailing list)
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
F3dc06f587d1ff4c7366b102bfda9204?d=identicon&s=25 David Mitchell (Guest)
on 2006-02-08 01:22
(Received via mailing list)
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.
2ff47d48fde7f8f2d1e25ab7ccc87d00?d=identicon&s=25 Peter Brown (Guest)
on 2006-02-08 01:41
(Received via mailing list)
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.
F3dc06f587d1ff4c7366b102bfda9204?d=identicon&s=25 David Mitchell (Guest)
on 2006-02-08 05:07
(Received via mailing list)
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.
This topic is locked and can not be replied to.