Single Table Inheritance validates_uniqueness_of Problem

Published on Author Akhil Bansal3 Comments

Consider a case of STI where:

class User < ActiveRecord::Base
  validates_uniqueness_of :name

class Customer < User


class Manager < User


Now try following at console:

User.create(:name => "Akhil Bansal")
Manager.create(:name => "Akhil Bansal")
Customer.create(:name => "Akhil Bansal")

This will let you create three records in users table with same name, validates_uniqueness_of written in User class has no effect on it. validates_uniqueness_of automatically scoped with class names, that means it will not let you create two managers with same name or two customers with same name or two users with same name.

If you want uniqueness of an attribute in overall table, put the following code in some file in your lib dir and require that file in environment:

module ActiveRecord
  module Validations
    module ClassMethods
      # Intended for use with STI tables, helps ignore the type field
      def validates_overall_uniqueness_of(*attr_names)
        configuration = { :message => "has already been taken" }
        configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
        validates_each(attr_names, configuration) do |record, attr_name, value|
          records = self.find(:all, :conditions=> ["#{attr_name} = ?", value])
          record.errors.add(attr_name, configuration[:message]) if records.size > 0 and records[0].id !=

And then use validates_overall_uniqueness_of instead of validates_uniqueness_of.

3 Responses to Single Table Inheritance validates_uniqueness_of Problem

Leave a Reply

Your email address will not be published. Required fields are marked *