ActiveRecord & Inheritance


#1

Hopefully a simple question…

I’ve got a table that, using the STI, contains some columns that are
specific to one subclass or another. How do I represent this fact in
Ruby? Specifically, in the following example, how do I keep someone
from creating an instance of Classb and assigning a value to typeconly
(an attribute that is only supposed to be applicable to objects of type
Classc)?

DATABASE:

create table classas (
id INTEGER AUTO_INCREMENT NOT NULL PRIMARY KEY,
type VARCHAR(10) NOT NULL,
genericattr VARCHAR(10) NOT NULL,
typebonly INTEGER, – for Classb instances only
typeconly VARCHAR(10) – for Classc instances only
);

RUBY:

class Classa < ActiveRecord::Base; end
class Classb < Classa

something that says attribute typebonly applies to this class only?

end
class Classc < Classa

something that says attribute typeconly applies to this class only?

end

Thanks.
-d