开发者

inserting superclass in rails, without corresponding database table

开发者 https://www.devze.com 2022-12-15 22:59 出处:网络
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:

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..开发者_开发问答.


Add a class method called abstract_class? that returns true:

class Vehicle < ActiveRecord::Base
  def self.abstract_class?
    true
  end
end
0

精彩评论

暂无评论...
验证码 换一张
取 消