I'm new to Rails, still getting my feet wet, so please pardon me if this is either trivial or "the wrong way" to do things.
I'd like to create a superclass for some scaffolded models. For example, I'd like to create a scaffold for Men
and for Women
, but I want them both to inherit from a People
superclass; Men
and Women
would inherit fields like height
and weight
from the People
class.
Where/how do I define this People
superclass? How do I define the subclasses Men
and Women
via scaffolding?
AFAIK you'd need to tweak the existing scaffolding templates, I don't believe there's a means to specify the controller base class. That said, I think in Rails 3 you can copy the templates into
$ROOT/lib/templates/rails/...
where...
depends on which you want to change.That said, what's the real goal in doing this in a scaffold? In general, models will (a) only rarely be subclasses, and (b) even more rarely be the same subclass.
Just edit them by hand.
Usually I do something like:
Important use the reserved word 'type'! That's where the table will keep which type the class is. Run the migration.
So, for the the subclasses you can do:
resulting Men:
Same for Women:
Resulting
No migration will be generated for the subclasses. I'm not sure but this approach only works for STI.
Hope it, helps!
watch this screencast on single table inheritance.
http://railscasts.com/episodes/394-sti-and-polymorphic-associations
Single table inheritance and where to use it in Rails
This is something I've thought about doing with my application. I haven't done it yet, and I wouldn't recommend it if you are new to rails. I would either make separate models entirely, or make one model, and have the attribute
gender
, which should be either a 0 or a 1, and then make a method that returns the string for the corresponding gender.EDIT
So I opened up the rails console, and from what I could see, it is possible totally possible, all you need to do is declare the class, and if you want to use different tables,
set_table_name
Or you could use one table, but if your trying to stay DRY, I would use two.
If you want to use the scaffold generator, you will have to run the typical
rails g scaffold Men
for each class you want views for (men and women). The model that this generates inherits from theActiveRecord::Base
class. The inheritance marker is the less than symbol (<
).You will then manually create the super class
User
and then edit the
Men
andWomen
models to inherit fromUser
lets say you wanted to subclass with one table, you could would right the migrations for that table, and then add the
attr_accessible
to the appropriate subclass.attr_accessible
is a rails security feature. It determines which attributes may be set in mass assignment. Anything related to security, site rank, etc. should not be accessible.Example:
because then someone could undermine your security system by passing
params => { :man => { :admin => true }}
The main point is that using these
attr_accessible
will determine which type of user can set what. Obviously you can DRY this up by putting shared features in the super-class. Hope this helpsYou should also read about the
super
keyword, and theself
keyword. If your running an inherited setup you will eventually want to use these.