diff options
author | Yves Senn <yves.senn@gmail.com> | 2015-08-20 12:07:02 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2015-08-20 12:07:02 +0200 |
commit | 50e4afff209ee6dcb50938b59bff32255a3f543e (patch) | |
tree | f1635d410e49db8db35ce86fbd5af9e83b3a23a1 /activerecord/lib/active_record/validations | |
parent | 013dd756e1ca730d95f13f334e02470ce528354b (diff) | |
download | rails-50e4afff209ee6dcb50938b59bff32255a3f543e.tar.gz rails-50e4afff209ee6dcb50938b59bff32255a3f543e.tar.bz2 rails-50e4afff209ee6dcb50938b59bff32255a3f543e.zip |
uniqueness validation raises error for persisted record without pk.
Closes #21304.
While we can validate uniqueness for record without primary key on
creation, there is no way to exclude the current record when
updating. (The update itself will need a primary key to work correctly).
Diffstat (limited to 'activerecord/lib/active_record/validations')
-rw-r--r-- | activerecord/lib/active_record/validations/uniqueness.rb | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index 32d17a1392..5706bbd903 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -18,7 +18,11 @@ module ActiveRecord relation = build_relation(finder_class, table, attribute, value) if record.persisted? && finder_class.primary_key.to_s != attribute.to_s - relation = relation.where.not(finder_class.primary_key => record.id) + if finder_class.primary_key + relation = relation.where.not(finder_class.primary_key => record.id) + else + raise UnknownPrimaryKey.new(finder_class, "Can not validate uniqueness for persisted record without primary key.") + end end relation = scope_relation(record, table, relation) relation = relation.merge(options[:conditions]) if options[:conditions] |