aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2015-07-25 13:05:05 -0600
committerSean Griffin <sean@thoughtbot.com>2015-07-25 13:05:53 -0600
commit119b9181ece399c67213543fb5227b82688b536f (patch)
treee490ef2e65f67ad884dac0f3f04d352f5edcb452 /activerecord/lib
parent9caf2cbf268137113447ac8156b862b5d84063b5 (diff)
downloadrails-119b9181ece399c67213543fb5227b82688b536f.tar.gz
rails-119b9181ece399c67213543fb5227b82688b536f.tar.bz2
rails-119b9181ece399c67213543fb5227b82688b536f.zip
Properly allow uniqueness validations on primary keys.
This is an alternate implementation of #20966. [Sean Griffin & presskey]
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/validations/uniqueness.rb4
1 files changed, 3 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb
index 5106f4e127..32d17a1392 100644
--- a/activerecord/lib/active_record/validations/uniqueness.rb
+++ b/activerecord/lib/active_record/validations/uniqueness.rb
@@ -17,7 +17,9 @@ module ActiveRecord
value = map_enum_attribute(finder_class, attribute, value)
relation = build_relation(finder_class, table, attribute, value)
- relation = relation.where.not(finder_class.primary_key => record.id) if record.persisted?
+ if record.persisted? && finder_class.primary_key.to_s != attribute.to_s
+ relation = relation.where.not(finder_class.primary_key => record.id)
+ end
relation = scope_relation(record, table, relation)
relation = relation.merge(options[:conditions]) if options[:conditions]