diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2007-10-23 06:18:42 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2007-10-23 06:18:42 +0000 |
commit | 28729a4829c68409717ecf82a6b407672ff5fa7d (patch) | |
tree | b4bb1841c1a787ce84227dd7045abc4b2d114b47 /activerecord/lib | |
parent | 6a35ed16c2c28c146a1da799983db56e3ca99e2e (diff) | |
download | rails-28729a4829c68409717ecf82a6b407672ff5fa7d.tar.gz rails-28729a4829c68409717ecf82a6b407672ff5fa7d.tar.bz2 rails-28729a4829c68409717ecf82a6b407672ff5fa7d.zip |
validates_uniqueness_of behaves well with abstract superclasses. References #3833, closes #9886.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8000 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-x | activerecord/lib/active_record/validations.rb | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 6514c4077a..35a1f8d0d4 100755 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -633,7 +633,21 @@ module ActiveRecord condition_params << record.send(:id) end - if find(:first, :conditions => [condition_sql, *condition_params]) + # The check for an existing value should be run from a class that + # isn't abstract. This means working down from the current class + # (self), to the first non-abstract class. Since classes don't know + # their subclasses, we have to build the hierarchy between self and + # the record's class. + class_hierarchy = [record.class] + while class_hierarchy.first != self + class_hierarchy.insert(0, class_hierarchy.first.superclass) + end + + # Now we can work our way down the tree to the first non-abstract + # class (which has a database table to query from). + finder_class = class_hierarchy.detect { |klass| !klass.abstract_class? } + + if finder_class.find(:first, :conditions => [condition_sql, *condition_params]) record.errors.add(attr_name, configuration[:message]) end end |