aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2013-10-27 13:05:19 -0700
committerRafael Mendonça França <rafaelmfranca@gmail.com>2013-10-27 13:05:19 -0700
commite17979e4f7cbf917d59a7cc515b77566e31afff6 (patch)
treea27ec11cec3fdb43a7e1bd042dfd304dde9fb3e6 /activerecord
parent16d81af5292c8f5f01cb38c862123fa01b995f3f (diff)
parent6b71a1416cb1457517b27cd0a55cb32df0cdf0c3 (diff)
downloadrails-e17979e4f7cbf917d59a7cc515b77566e31afff6.tar.gz
rails-e17979e4f7cbf917d59a7cc515b77566e31afff6.tar.bz2
rails-e17979e4f7cbf917d59a7cc515b77566e31afff6.zip
Merge pull request #12664 from jetthoughts/12242_includes_in_through_association
Skip `include_values` from through associations chains for building association scope
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md24
-rw-r--r--activerecord/lib/active_record/associations/association_scope.rb8
-rw-r--r--activerecord/test/cases/associations/has_many_through_associations_test.rb4
-rw-r--r--activerecord/test/models/post.rb3
4 files changed, 37 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 0c325900e8..a117c36ef1 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,27 @@
+* Do not add to scope includes values from through associations.
+ Fixed bug when providing `includes` in through association scope, and fetching targets.
+
+ Example:
+ class Vendor < ActiveRecord::Base
+ has_many :relationships, -> { includes(:user) }
+ has_many :users, through: :relationships
+ end
+
+ vendor = Vendor.first
+
+ # Before
+
+ vendor.users.to_a # => Raises exception: not found `:user` for `User`
+
+ # After
+
+ vendor.users.to_a # => No exception is raised
+
+
+ Fixes: #12242, #9517, #10240
+
+ *Paul Nikitochkin*
+
* Type cast json values on write, so that the value is consistent
with reading from the database.
diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/active_record/associations/association_scope.rb
index d862a5f29d..17f056e764 100644
--- a/activerecord/lib/active_record/associations/association_scope.rb
+++ b/activerecord/lib/active_record/associations/association_scope.rb
@@ -78,7 +78,8 @@ module ActiveRecord
scope = scope.joins(join(foreign_table, constraint))
end
- klass = i == 0 ? self.klass : reflection.klass
+ is_first_chain = i == 0
+ klass = is_first_chain ? self.klass : reflection.klass
# Exclude the scope of the association itself, because that
# was already merged in the #scope method.
@@ -89,7 +90,10 @@ module ActiveRecord
scope.merge! item.except(:where, :includes, :bind)
end
- scope.includes! item.includes_values
+ if is_first_chain
+ scope.includes! item.includes_values
+ end
+
scope.where_values += item.where_values
scope.order_values |= item.order_values
end
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 3b61b91d62..b1e823729e 100644
--- a/activerecord/test/cases/associations/has_many_through_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -1085,4 +1085,8 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
readers(:michael_authorless).update(first_post_id: 1)
assert_equal [posts(:thinking)], person.reload.first_posts
end
+
+ def test_has_many_through_with_includes_in_through_association_scope
+ posts(:welcome).author_address_extra_with_address.to_a
+ end
end
diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb
index 452d54580a..faf539a562 100644
--- a/activerecord/test/models/post.rb
+++ b/activerecord/test/models/post.rb
@@ -65,6 +65,9 @@ class Post < ActiveRecord::Base
has_many :author_favorites, :through => :author
has_many :author_categorizations, :through => :author, :source => :categorizations
has_many :author_addresses, :through => :author
+ has_many :author_address_extra_with_address,
+ through: :author_with_address,
+ source: :author_address_extra
has_many :comments_with_interpolated_conditions,
->(p) { where "#{"#{p.aliased_table_name}." rescue ""}body = ?", 'Thank you for the welcome' },