diff options
author | Godfrey Chan <godfreykfc@gmail.com> | 2014-09-05 04:27:54 -0700 |
---|---|---|
committer | Godfrey Chan <godfreykfc@gmail.com> | 2014-09-05 04:37:12 -0700 |
commit | a8827cb9afdf122c003fbff1bfcc28bda2994d04 (patch) | |
tree | 20e33bea1ad37c74358f217496ee76870624db68 /activerecord | |
parent | ce43e8a6fcca0df88b592955b4b183301e5f3d6d (diff) | |
download | rails-a8827cb9afdf122c003fbff1bfcc28bda2994d04.tar.gz rails-a8827cb9afdf122c003fbff1bfcc28bda2994d04.tar.bz2 rails-a8827cb9afdf122c003fbff1bfcc28bda2994d04.zip |
Fixed regression with referencing polymorphic assoc in eager-load
This is cased by 03118bc + 9b5d603. The first commit referenced the undefined
local variable `column` when it should be using `reflection.type` as the lookup
key. The second commit changed `build_arel` to not modify the `bind_values` in-
place so we need to combine the arel's `bind_values` with the relation's when
building the SQL.
Fixes #16591
Related #15821 / #15892 / 7aeca50
Diffstat (limited to 'activerecord')
3 files changed, 13 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/join_dependency/join_association.rb b/activerecord/lib/active_record/associations/join_dependency/join_association.rb index c3bbdccad8..e7d3c9ba40 100644 --- a/activerecord/lib/active_record/associations/join_dependency/join_association.rb +++ b/activerecord/lib/active_record/associations/join_dependency/join_association.rb @@ -65,7 +65,7 @@ module ActiveRecord if reflection.type value = foreign_klass.base_class.name - column = klass.columns_hash[column.to_s] + column = klass.columns_hash[reflection.type.to_s] substitute = klass.connection.substitute_at(column, bind_values.length) bind_values.push [column, value] diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index a7899da3a8..b1753b27e1 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -401,8 +401,9 @@ module ActiveRecord "#{quoted_table_name}.#{quoted_primary_key}", relation.order_values) relation = relation.except(:select).select(values).distinct! + arel = relation.arel - id_rows = @klass.connection.select_all(relation.arel, 'SQL', relation.bind_values) + id_rows = @klass.connection.select_all(arel, 'SQL', arel.bind_values + relation.bind_values) id_rows.map {|row| row[primary_key]} end diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 21912fdf0f..b852bd3536 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -1290,4 +1290,14 @@ class EagerAssociationTest < ActiveRecord::TestCase david = Author.where(id: "1").eager_load(:readonly_comments).first! assert david.readonly_comments.first.readonly? end + + test "preloading a polymorphic association with references to the associated table" do + post = Post.includes(:tags).references(:tags).where('tags.name = ?', 'General').first + assert_equal posts(:welcome), post + end + + test "eager-loading a polymorphic association with references to the associated table" do + post = Post.eager_load(:tags).where('tags.name = ?', 'General').first + assert_equal posts(:welcome), post + end end |