Validates_uniqueness_of and single table inheritance
May 18, 2007
If you’re using STI (single table inheritance) with some Ruby on Rails models, and you need to make sure some fields are unique across all the models, you can use validates_uniqueness_of in the parent model using the :scope option to apply it to all the models. So in this instance since you have a type field in the table, you would use:
validates_uniqueness_of :name, :scope => :type
However, you might encounter trouble with type being a deprecated method that is aliased to class. The fix is to add this before the validation:
undef_method :type
Filed under: Ruby on Rails |