Rails STI - Prevent base class from instantiation

前端 未结 6 935
陌清茗
陌清茗 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:34

    even better than validating the presence is validating against the known list of accepted non abstract class types

      validates :type, :inclusion=> { :in => ["A", "B", "C"] }
    

    because if you validate just for presence an "evil developer" can still pass in the abstract class name as the type parameter.

提交回复
热议问题