Skip to content

Commit

Permalink
Chained scopes will be preloaded properly. Fixes rails#7490
Browse files Browse the repository at this point in the history
  • Loading branch information
seejee authored and Chris Geihsler committed Apr 3, 2013
1 parent 4886991 commit 453c7d6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,5 +1,10 @@
## Rails 3.2.13 (Feb 17, 2013) ## ## Rails 3.2.13 (Feb 17, 2013) ##


* Chaining multiple preloaded scopes will correctly preload all the scopes
at the same time.

*Chris Geihsler*

* Reverted 921a296a3390192a71abeec6d9a035cc6d1865c8, 'Quote numeric values * Reverted 921a296a3390192a71abeec6d9a035cc6d1865c8, 'Quote numeric values
compared to string columns.' This caused several regressions. compared to string columns.' This caused several regressions.


Expand Down
9 changes: 7 additions & 2 deletions activerecord/lib/active_record/relation/spawn_methods.rb
Expand Up @@ -17,14 +17,14 @@ def merge(r)
if method == :includes if method == :includes
merged_relation = merged_relation.includes(value) merged_relation = merged_relation.includes(value)
else else
merged_relation.send(:"#{method}_values=", value) merge_relation_method(merged_relation, method, value)
end end
end end
end end


(Relation::MULTI_VALUE_METHODS - [:joins, :where, :order]).each do |method| (Relation::MULTI_VALUE_METHODS - [:joins, :where, :order]).each do |method|
value = r.send(:"#{method}_values") value = r.send(:"#{method}_values")
merged_relation.send(:"#{method}_values=", merged_relation.send(:"#{method}_values") + value) if value.present? merge_relation_method(merged_relation, method, value) if value.present?
end end


merged_relation.joins_values += r.joins_values merged_relation.joins_values += r.joins_values
Expand Down Expand Up @@ -144,5 +144,10 @@ def apply_finder_options(options)
relation relation
end end


private

def merge_relation_method(relation, method, value)
relation.send(:"#{method}_values=", relation.send(:"#{method}_values") + value)
end
end end
end end
7 changes: 7 additions & 0 deletions activerecord/test/cases/relations_test.rb
Expand Up @@ -332,6 +332,13 @@ def test_find_with_preloaded_associations
end end
end end


def test_preload_applies_to_all_chained_preloaded_scopes
assert_queries(3) do
post = Post.with_tags.with_comments.first
assert post
end
end

def test_find_with_included_associations def test_find_with_included_associations
assert_queries(2) do assert_queries(2) do
posts = Post.includes(:comments).order('posts.id') posts = Post.includes(:comments).order('posts.id')
Expand Down
3 changes: 3 additions & 0 deletions activerecord/test/models/post.rb
Expand Up @@ -76,6 +76,9 @@ def add_joins_and_select
end end
end end


scope :with_comments, preload(:comments)
scope :with_tags, preload(:taggings)

has_many :interpolated_taggings, :class_name => 'Tagging', :as => :taggable, :conditions => proc { "1 = #{1}" } has_many :interpolated_taggings, :class_name => 'Tagging', :as => :taggable, :conditions => proc { "1 = #{1}" }
has_many :interpolated_tags, :through => :taggings has_many :interpolated_tags, :through => :taggings
has_many :interpolated_tags_2, :through => :interpolated_taggings, :source => :tag has_many :interpolated_tags_2, :through => :interpolated_taggings, :source => :tag
Expand Down

0 comments on commit 453c7d6

Please sign in to comment.