1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix regression caused by a01d164b

When preload is used in a default scope the preload_values were
returning nested arrays and causing the preloader to fail because it
doesn't know how to deal with nested arrays. So before calling preload!
we need to splat the arguments.

This is not needed to includes because it flatten its arguments.
This commit is contained in:
Rafael Mendonça França 2015-07-07 13:38:54 -03:00
parent 4cc5917dce
commit 1b4399dfe7
3 changed files with 37 additions and 2 deletions

View file

@ -87,8 +87,8 @@ module ActiveRecord
return if other.preload_values.empty? && other.includes_values.empty? return if other.preload_values.empty? && other.includes_values.empty?
if other.klass == relation.klass if other.klass == relation.klass
relation.preload! other.preload_values unless other.preload_values.empty? relation.preload!(*other.preload_values) unless other.preload_values.empty?
relation.includes! other.includes_values unless other.includes_values.empty? relation.includes!(other.includes_values) unless other.includes_values.empty?
else else
reflection = relation.klass.reflect_on_all_associations.find do |r| reflection = relation.klass.reflect_on_all_associations.find do |r|
r.class_name == other.klass.name r.class_name == other.klass.name

View file

@ -647,6 +647,25 @@ class RelationTest < ActiveRecord::TestCase
end end
end end
def test_preloading_with_associations_default_scopes_and_merges
post = Post.create! title: 'Uhuu', body: 'body'
reader = Reader.create! post_id: post.id, person_id: 1
post_rel = PostWithPreloadDefaultScope.preload(:readers).joins(:readers).where(title: 'Uhuu')
result_post = PostWithPreloadDefaultScope.all.merge(post_rel).to_a.first
assert_no_queries do
assert_equal [reader], result_post.readers.to_a
end
post_rel = PostWithIncludesDefaultScope.includes(:readers).where(title: 'Uhuu')
result_post = PostWithIncludesDefaultScope.all.merge(post_rel).to_a.first
assert_no_queries do
assert_equal [reader], result_post.readers.to_a
end
end
def test_loading_with_one_association def test_loading_with_one_association
posts = Post.preload(:comments) posts = Post.preload(:comments)
post = posts.find { |p| p.id == 1 } post = posts.find { |p| p.id == 1 }

View file

@ -208,6 +208,22 @@ class PostWithDefaultScope < ActiveRecord::Base
default_scope { order(:title) } default_scope { order(:title) }
end end
class PostWithPreloadDefaultScope < ActiveRecord::Base
self.table_name = 'posts'
has_many :readers, foreign_key: 'post_id'
default_scope { preload(:readers) }
end
class PostWithIncludesDefaultScope < ActiveRecord::Base
self.table_name = 'posts'
has_many :readers, foreign_key: 'post_id'
default_scope { includes(:readers) }
end
class SpecialPostWithDefaultScope < ActiveRecord::Base class SpecialPostWithDefaultScope < ActiveRecord::Base
self.table_name = 'posts' self.table_name = 'posts'
default_scope { where(:id => [1, 5,6]) } default_scope { where(:id => [1, 5,6]) }