Search code examples
ruby-on-railsruby-on-rails-5activemodelsingle-table-inheritancesti

Should I validate the type column? (Rails, STI)


I have a model and I am using Single Table Inheritance. This has a type column in my database which is populated with the string of the class name.

Should I validate this column?

Options:

  • required column in the db
  • validates :type, presence: true
  • validates :type, inclusion: {in: [Class1.to_s, Class2.to_s]}

Solution

  • Since ActiveRecord handles the type field you don't need to write extra validations on this field.