views:

33

answers:

1

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:

  1. Make class Vehicle < ActiveRecord::Base, and have class Truck < Vehicle etc. But then I get errors saying I don't have a table for Vehicle (and I don't want one, either).

  2. Use module Vehicle and include Vehicle in class Truck < ActiveRecord::Base. But then attr_reader and friends don't get applied to Truck.

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...

+4  A: 

Add a class method called abstract_class? that returns true:

class Vehicle < ActiveRecord::Base
  def self.abstract_class?
    true
  end
end
khelll
Ah, perfect, I knew this had to have a clean solution. Thanks!
Peter