Its all about Ruby On Rails
Single Table Inheritance validates_uniqueness_of Problem
Consider a case of STI where:
class User < ActiveRecord::Base validates_uniqueness_of :name end class Customer < User end class Manager < User end
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 != record.id
end
end
end
end
end
And then use validates_overall_uniqueness_of instead of validates_uniqueness_of.
| Print article | This entry was posted by Akhil Bansal on October 26, 2007 at 1:15 pm, and is filed under ROR, Rails, Rubyonrails, validates_uniqueness. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 2 years ago
When you define methods this way, there is no such problem:
class User
about 2 years ago
Thanks,
I got this error and your solution worked here.
about 2 years ago
You are the man