Rails STI - Prevent base class from instantiation

前端 未结 6 920
陌清茗
陌清茗 2021-01-01 11:06

Is there any way in a Rails STI situation to throw an error when the base class is Instantiated? Overriding initialize will do it but then that gets trickled down to the sub

6条回答
  •  借酒劲吻你
    2021-01-01 11:39

    I often prefer to simply make the new class method private with:

    class Base
      private_class_method :new 
    end
    

    This way accidental instantiation of the Base class triggers an error, but it's still possible to instantiate it with Base.send(:new) to write tests for the Base class.

提交回复
热议问题