Forum: Ruby on Rails ActiveRecord & Inheritance

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.
63d11618e9cd62b7c5a8ac93b63111c2?d=identicon&s=25 Dave Wood (davewood)
on 2005-11-26 23:29
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
This topic is locked and can not be replied to.