diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-10-27 22:07:41 -0200 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-10-27 22:07:41 -0200 |
commit | 0ef2256c4a7635e78126cce4aa2c7467d2fb414f (patch) | |
tree | 6c6471e9dac32d303a93c5cde655123f4eef6faf /activerecord | |
parent | c19d664380199dac1474c303cf4f8b416986f106 (diff) | |
parent | 33a5416461bc5068d85d33259310f56625569e86 (diff) | |
download | rails-0ef2256c4a7635e78126cce4aa2c7467d2fb414f.tar.gz rails-0ef2256c4a7635e78126cce4aa2c7467d2fb414f.tar.bz2 rails-0ef2256c4a7635e78126cce4aa2c7467d2fb414f.zip |
Merge pull request #18383 from scambra/habtm-with-where-includes-16032-for-master
Includes HABTM returns correct size now
Diffstat (limited to 'activerecord')
3 files changed, 40 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 5e523d2aa6..7055cb2e25 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,22 @@ +* Includes HABTM returns correct size now. It's caused by the join dependency + only instantiates one HABTM object because the join table hasn't a primary key. + + Fixes #16032. + + Examples: + + before: + + Project.first.salaried_developers.size # => 3 + Project.includes(:salaried_developers).first.salaried_developers.size # => 1 + + after: + + Project.first.salaried_developers.size # => 3 + Project.includes(:salaried_developers).first.salaried_developers.size # => 3 + + *Bigxiang* + * Add option to index errors in nested attributes For models which have nested attributes, errors within those models will diff --git a/activerecord/lib/active_record/associations/join_dependency.rb b/activerecord/lib/active_record/associations/join_dependency.rb index ca473beea3..0e98a3b3a4 100644 --- a/activerecord/lib/active_record/associations/join_dependency.rb +++ b/activerecord/lib/active_record/associations/join_dependency.rb @@ -150,7 +150,8 @@ module ActiveRecord message_bus.instrument('instantiation.active_record', payload) do result_set.each { |row_hash| - parent = parents[row_hash[primary_key]] ||= join_root.instantiate(row_hash, column_aliases) + parent_key = primary_key ? row_hash[primary_key] : row_hash + parent = parents[parent_key] ||= join_root.instantiate(row_hash, column_aliases) construct(parent, join_root, row_hash, result_set, seen, model_cache, aliases) } end diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index 20af436e02..e9f679e6de 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -957,4 +957,23 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase projects = ProjectUnscopingDavidDefaultScope.includes(:developers).where(id: project.id) assert_equal 1, projects.first.developers.size end + + def test_preloaded_associations_size + assert_equal Project.first.salaried_developers.size, + Project.preload(:salaried_developers).first.salaried_developers.size + + assert_equal Project.includes(:salaried_developers).references(:salaried_developers).first.salaried_developers.size, + Project.preload(:salaried_developers).first.salaried_developers.size + + # Nested HATBM + first_project = Developer.first.projects.first + preloaded_first_project = + Developer.preload(projects: :salaried_developers). + first. + projects. + detect { |p| p.id == first_project.id } + + assert preloaded_first_project.salaried_developers.loaded?, true + assert_equal first_project.salaried_developers.size, preloaded_first_project.salaried_developers.size + end end |