Browse files

Nested through associations: preloads from the default scope of a thr…

…ough model should not be included in the association scope. (We're already excluding includes.) Fixes #2834.
  • Loading branch information...
1 parent a48e332 commit d4b9829a055399884d0d19f0b723db1681eba4ed @jonleighton jonleighton committed Sep 6, 2011
View
3 activerecord/CHANGELOG
@@ -1,5 +1,8 @@
*Rails 3.1.1 (unreleased)*
+* Fix clash between using 'preload' in a default scope and including the default scoped model
+ in a nested through association. (GH #2834.) [Jon Leighton]
+
* Ensure we are not comparing a string with a symbol in HasManyAssociation#inverse_updates_counter_cache?.
Fixes GH #2755, where a counter cache could be decremented twice as far as it was supposed to be.
View
2 activerecord/lib/active_record/associations/through_association.rb
@@ -16,7 +16,7 @@ def target_scope
chain[1..-1].each do |reflection|
scope = scope.merge(
reflection.klass.scoped.with_default_scope.
- except(:select, :create_with, :includes)
+ except(:select, :create_with, :includes, :preload)
)
end
scope
View
9 activerecord/test/cases/associations/nested_through_associations_test.rb
@@ -356,6 +356,15 @@ def test_has_one_through_has_one_through_with_belongs_to_source_reflection
assert_equal categories(:general), members(:groucho).club_category
end
+ def test_has_one_through_has_one_through_with_belongs_to_source_reflection_with_default_scope_preload_on_the_through_model
+ prev_default_scope = Club.default_scopes
+ Club.default_scopes = [Club.preload(:category)]
+
+ assert_equal categories(:general), members(:groucho).club_category
+ ensure
+ Club.default_scopes = prev_default_scope
+ end
+
def test_has_one_through_has_one_through_with_belongs_to_source_reflection_preload
members = assert_queries(4) { Member.includes(:club_category).to_a.sort_by(&:id) }
general = categories(:general)

0 comments on commit d4b9829

Please sign in to comment.