aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorHimesh <himesh.rupareliya@gmail.com>2015-07-23 15:49:22 +0530
committerArthur Neves <arthurnn@gmail.com>2016-02-02 23:36:31 -0500
commit9c9fb19b9eda46a76d7ce4fd6cb9cc94bd965e62 (patch)
treee85a708ffcf2b8790c611716aa7769ddc23b4875 /activerecord
parent0e189cb3df7899bd99697a40b9e5f6316299ac04 (diff)
downloadrails-9c9fb19b9eda46a76d7ce4fd6cb9cc94bd965e62.tar.gz
rails-9c9fb19b9eda46a76d7ce4fd6cb9cc94bd965e62.tar.bz2
rails-9c9fb19b9eda46a76d7ce4fd6cb9cc94bd965e62.zip
Changed id-writer to save join table records based on association primary key #20995
Changed id-writer to save join table records based on association primary key
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/collection_association.rb5
-rw-r--r--activerecord/test/cases/associations/has_many_through_associations_test.rb2
2 files changed, 5 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb
index 87576abd92..333dd4f180 100644
--- a/activerecord/lib/active_record/associations/collection_association.rb
+++ b/activerecord/lib/active_record/associations/collection_association.rb
@@ -72,7 +72,10 @@ module ActiveRecord
pk_type = reflection.primary_key_type
ids = Array(ids).reject(&:blank?)
ids.map! { |i| pk_type.cast(i) }
- replace(klass.find(ids).index_by(&:id).values_at(*ids))
+ records = klass.where(reflection.association_primary_key => ids).index_by do |r|
+ r.send(reflection.association_primary_key)
+ end.values_at(*ids)
+ replace(records)
end
def reset
diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb
index ce2557339e..20ced1feb5 100644
--- a/activerecord/test/cases/associations/has_many_through_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -882,7 +882,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
def test_collection_singular_ids_setter_raises_exception_when_invalid_ids_set
company = companies(:rails_core)
ids = [Developer.first.id, -9999]
- assert_raises(ActiveRecord::RecordNotFound) {company.developer_ids= ids}
+ assert_raises(ActiveRecord::AssociationTypeMismatch) {company.developer_ids= ids}
end
def test_build_a_model_from_hm_through_association_with_where_clause