mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Allow preload and eager_load to work on relations at the same time.
This commit is contained in:
parent
c3b4da7796
commit
1d5854826b
2 changed files with 30 additions and 6 deletions
|
@ -26,9 +26,9 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def to_a
|
||||
if @eager_load_associations.any?
|
||||
records = catch :invalid_query do
|
||||
@klass.send(:find_with_associations, {
|
||||
records = if @eager_load_associations.any?
|
||||
catch :invalid_query do
|
||||
return @klass.send(:find_with_associations, {
|
||||
:select => @relation.send(:select_clauses).join(', '),
|
||||
:joins => @relation.joins(relation),
|
||||
:group => @relation.send(:group_clauses).join(', '),
|
||||
|
@ -38,11 +38,13 @@ module ActiveRecord
|
|||
},
|
||||
ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, @eager_load_associations, nil))
|
||||
end
|
||||
[]
|
||||
else
|
||||
records = @klass.find_by_sql(@relation.to_sql)
|
||||
@klass.find_by_sql(@relation.to_sql)
|
||||
end
|
||||
|
||||
@klass.send(:preload_associations, records, @associations_to_preload) unless @associations_to_preload.empty?
|
||||
records.each { |record| record.readonly! } if @readonly
|
||||
end
|
||||
|
||||
records
|
||||
end
|
||||
|
|
|
@ -4,6 +4,7 @@ require 'models/topic'
|
|||
require 'models/comment'
|
||||
require 'models/reply'
|
||||
require 'models/author'
|
||||
require 'models/comment'
|
||||
require 'models/entrant'
|
||||
require 'models/developer'
|
||||
require 'models/company'
|
||||
|
@ -125,5 +126,26 @@ class RelationTest < ActiveRecord::TestCase
|
|||
assert_equal 'Jamis', DeveloperCalledJamis.create!.name
|
||||
end
|
||||
|
||||
def test_loading_with_one_association
|
||||
posts = Post.all(:include => :comments).to_a
|
||||
post = posts.find { |p| p.id == 1 }
|
||||
assert_equal 2, post.comments.size
|
||||
assert post.comments.include?(comments(:greetings))
|
||||
|
||||
post = Post.find(:first, :include => :comments, :conditions => "posts.title = 'Welcome to the weblog'")
|
||||
assert_equal 2, post.comments.size
|
||||
assert post.comments.include?(comments(:greetings))
|
||||
|
||||
posts = Post.all(:include => :last_comment).to_a
|
||||
post = posts.find { |p| p.id == 1 }
|
||||
assert_equal Post.find(1).last_comment, post.last_comment
|
||||
end
|
||||
|
||||
def test_loading_with_one_association_with_non_preload
|
||||
posts = Post.all(:include => :last_comment, :order => 'comments.id DESC').to_a
|
||||
post = posts.find { |p| p.id == 1 }
|
||||
assert_equal Post.find(1).last_comment, post.last_comment
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue