I'd like to have a common superclass for two models in ruby, to share several methods. Let's say I want a Truck
and Car
inheriting from Vehicle
. Here are some options:
Make
class Vehicle < ActiveRecord::Base
, and haveclass Truck < Vehicle
etc. But then I get errors saying I don't have a table forVehicle
(and I don't want one, either).Use
module Vehicle
andinclude Vehicle
inclass Truck < ActiveRecord::Base
. But thenattr_reader
and friends don't get applied toTruck
.
Thus, I want class Vehicle
. How do I do this without requiring a table? I'm sure there's a standard, nice way of doing this..开发者_开发问答.
Add a class method called abstract_class?
that returns true
:
class Vehicle < ActiveRecord::Base
def self.abstract_class?
true
end
end
精彩评论