Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix preloading of st models on has_many through associations #14312

Open
wants to merge 1 commit into from

4 participants

@moktin

This patch fixes #11078 by removing the type conditions from through_scope.
It is really inspired by #11082 and the comment from @neerajdotname.

@pftg pftg commented on the diff
activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
+
+* Fix preloading on has_many :through association in combination with
+ STI models.
+
+ Fixes #11078.
+
@pftg
pftg added a note

Please add your name here :wink:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@pftg pftg commented on the diff
activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
+
@pftg
pftg added a note

:scissors:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@mikegee

@moktin I think @pftg is asking for you to remove the blank lines with the :scissors:

Also, you will probably be asked to swash these two commits and force-push before this can be merged.

Please either address these two things, or close this if you are no longer concerned with it.

Thanks!

@moktin moktin Fix preloading of sti models on has_many through associations
This patch fixes #11078 by removing the type conditions from through_scope.
It is really inspired by #11082 and the comment from @neerajdotname.
f62fa79
@moktin

HI @mikegee,
Thank you for the feedback.

I squashed these two commits and pushed them.
The blank line was already removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 9, 2014
  1. @moktin

    Fix preloading of sti models on has_many through associations

    moktin authored
    This patch fixes #11078 by removing the type conditions from through_scope.
    It is really inspired by #11082 and the comment from @neerajdotname.
This page is out of date. Refresh to see the latest.
View
7 activerecord/CHANGELOG.md
@@ -1,3 +1,10 @@
+* Fix preloading on has_many :through association in combination with
+ STI models.
+
@pftg
pftg added a note

:scissors:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ Fixes #11078.
+
@pftg
pftg added a note

Please add your name here :wink:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ *Hector Satre*
+
* Allow strings to specify the `#order` value.
Example:
View
9 activerecord/lib/active_record/associations/preloader/through_association.rb
@@ -80,7 +80,14 @@ def through_scope
else
unless reflection_scope.where_values.empty?
scope.includes_values = Array(reflection_scope.values[:includes] || options[:source])
- scope.where_values = reflection_scope.values[:where]
+
+ base_wheres = if reflection.klass.finder_needs_type_condition?
+ reflection.klass.send(:relation_with_type_condition).values[:where]
+ else
+ []
+ end
+
+ scope.where_values = reflection_scope.values[:where] - base_wheres
end
scope.references! reflection_scope.values[:references]
View
16 activerecord/lib/active_record/core.rb
@@ -164,14 +164,22 @@ def arel_engine # :nodoc:
private
def relation #:nodoc:
- relation = Relation.create(self, arel_table)
-
if finder_needs_type_condition?
- relation.where(type_condition).create_with(inheritance_column.to_sym => sti_name)
+ relation_with_type_condition
else
- relation
+ relation_without_type_condition
end
end
+
+ def relation_with_type_condition #:nodoc:
+ relation = relation_without_type_condition
+
+ relation.where(type_condition).create_with(inheritance_column.to_sym => sti_name)
+ end
+
+ def relation_without_type_condition #:nodoc:
+ Relation.create(self, arel_table)
+ end
end
# New objects can be instantiated as either empty (pass no construction parameter) or pre-set with
View
8 activerecord/test/cases/associations/eager_test.rb
@@ -482,6 +482,14 @@ def test_eager_with_has_many_through_an_sti_join_model
assert_equal [comments(:does_it_hurt)], assert_no_queries { author.special_post_comments }
end
+ def test_preloading_has_many_through_with_association_inheritance
+ authors = Author.includes(:very_special_comments).to_a
+ assert_no_queries do
+ special_comment_authors = authors.map { |author| [author.name, author.very_special_comments.size]}
+ assert_equal [["David", 1], ["Mary", 0], ["Bob", 0]], special_comment_authors
+ end
+ end
+
def test_eager_with_has_many_through_an_sti_join_model_with_conditions_on_both
author = Author.all.merge!(:includes => :special_nonexistant_post_comments, :order => 'authors.id').first
assert_equal [], author.special_nonexistant_post_comments
Something went wrong with that request. Please try again.