Rails STI validation inheritance

2019-07-18 14:38发布

I have STI models in my Rails application. The ancestor model has validations with the validates_... methods which are working fine.

But I have custom validations as well, and I would like to add more different custom validations in the descendants. These custom validations would depend on the class.

If I write

class DescendantA < Ancestor

  protected
    def validate
      # ...
    end
end

It simply overwrites the original validations, so I loose the original inherited validations.

Is there a convention to do this in Rails?

1条回答
狗以群分
2楼-- · 2019-07-18 15:34

Would it not be sufficient to add super at the end of the validate method in the child, so that the parent's validate method would also be called?

查看更多
登录 后发表回答