diff options
author | Dominic Cleal <dominic@cleal.org> | 2016-10-07 08:56:05 +0100 |
---|---|---|
committer | Dominic Cleal <dominic@cleal.org> | 2016-11-24 09:29:40 +0000 |
commit | 935502062e647def60288944808240667f7893cc (patch) | |
tree | 0d00de76bc70f2808a0d78185eca3db1790674f5 /activerecord/lib | |
parent | 15e2da656f41af0124f7577858536f3b65462ad5 (diff) | |
download | rails-935502062e647def60288944808240667f7893cc.tar.gz rails-935502062e647def60288944808240667f7893cc.tar.bz2 rails-935502062e647def60288944808240667f7893cc.zip |
Add test for collection *_ids= setter when association primary key set
Fixes casting of IDs to the data type of the association primary key,
rather than then the data type of the model's primary key. (Tests use a
string primary key on the association, rather than an int.)
Tests issue #20995
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/collection_association.rb | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/reflection.rb | 4 |
2 files changed, 5 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 99e6696c66..3d23fa1e46 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -68,7 +68,7 @@ module ActiveRecord # Implements the ids writer method, e.g. foo.item_ids= for Foo.has_many :items def ids_writer(ids) - pk_type = reflection.primary_key_type + pk_type = reflection.association_primary_key_type ids = Array(ids).reject(&:blank?) ids.map! { |i| pk_type.cast(i) } records = klass.where(reflection.association_primary_key => ids).index_by do |r| diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index ef3c3bfae8..17751c9571 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -397,6 +397,10 @@ module ActiveRecord options[:primary_key] || primary_key(klass || self.klass) end + def association_primary_key_type + klass.type_for_attribute(association_primary_key) + end + def active_record_primary_key @active_record_primary_key ||= options[:primary_key] || primary_key(active_record) end |