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

Eager Loading support added for has_many :through => :has_many associations (see below). [Rick Olson]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3967 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Rick Olson 2006-03-19 03:05:21 +00:00
parent 3f77a04cb5
commit 9412c0531d
2 changed files with 24 additions and 6 deletions

View file

@ -1244,7 +1244,7 @@ module ActiveRecord
class JoinAssociation < JoinBase class JoinAssociation < JoinBase
attr_reader :reflection, :parent, :aliased_table_name, :aliased_prefix, :aliased_join_table_name, :parent_table_name attr_reader :reflection, :parent, :aliased_table_name, :aliased_prefix, :aliased_join_table_name, :parent_table_name
delegate :options, :klass, :to => :reflection delegate :options, :klass, :through_reflection, :source_reflection, :to => :reflection
def initialize(reflection, join_dependency, parent = nil) def initialize(reflection, join_dependency, parent = nil)
reflection.check_validity! reflection.check_validity!
@ -1291,7 +1291,6 @@ module ActiveRecord
when :has_many, :has_one when :has_many, :has_one
case case
when reflection.macro == :has_many && reflection.options[:through] when reflection.macro == :has_many && reflection.options[:through]
through_reflection = parent.active_record.reflect_on_association(reflection.options[:through])
through_conditions = through_reflection.options[:conditions] ? "AND #{interpolate_sql(sanitize_sql(through_reflection.options[:conditions]))}" : '' through_conditions = through_reflection.options[:conditions] ? "AND #{interpolate_sql(sanitize_sql(through_reflection.options[:conditions]))}" : ''
if through_reflection.options[:as] # has_many :through against a polymorphic join if through_reflection.options[:as] # has_many :through against a polymorphic join
polymorphic_foreign_key = through_reflection.options[:as].to_s + '_id' polymorphic_foreign_key = through_reflection.options[:as].to_s + '_id'
@ -1305,16 +1304,26 @@ module ActiveRecord
" LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [table_name_and_alias, " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [table_name_and_alias,
aliased_table_name, primary_key, aliased_join_table_name, options[:foreign_key] || reflection.klass.to_s.classify.foreign_key aliased_table_name, primary_key, aliased_join_table_name, options[:foreign_key] || reflection.klass.to_s.classify.foreign_key
] ]
else # has_many :through against a normal join elsif source_reflection.macro == :belongs_to # has_many :through against a belongs_to
" LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [
table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), aliased_join_table_name, table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), aliased_join_table_name,
through_reflection.options[:foreign_key] || through_reflection.active_record.to_s.classify.foreign_key, through_reflection.primary_key_name,
parent.aliased_table_name, parent.primary_key] + parent.aliased_table_name, parent.primary_key] +
" LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [
table_name_and_alias, table_name_and_alias,
aliased_table_name, primary_key, aliased_table_name, primary_key,
aliased_join_table_name, options[:foreign_key] || klass.to_s.classify.foreign_key aliased_join_table_name, options[:foreign_key] || klass.to_s.classify.foreign_key
] ]
elsif source_reflection.macro == :has_many # has_many :through against a has_many
" LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [
table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), aliased_join_table_name,
through_reflection.primary_key_name,
parent.aliased_table_name, parent.primary_key] +
" LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [
table_name_and_alias,
aliased_table_name, through_reflection.klass.to_s.classify.foreign_key,
aliased_join_table_name, options[:foreign_key] || primary_key
]
end end
when reflection.macro == :has_many && reflection.options[:as] when reflection.macro == :has_many && reflection.options[:as]
@ -1325,6 +1334,7 @@ module ActiveRecord
aliased_table_name, "#{reflection.options[:as]}_type", aliased_table_name, "#{reflection.options[:as]}_type",
klass.quote(parent.active_record.base_class.name) klass.quote(parent.active_record.base_class.name)
] ]
else else
" LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [
table_name_and_alias, table_name_and_alias,

View file

@ -231,11 +231,11 @@ class AssociationsJoinModelTest < Test::Unit::TestCase
end end
def test_has_many_through_has_many_find_all def test_has_many_through_has_many_find_all
assert_equal comments(:greetings), authors(:david).comments.find(:all).first assert_equal comments(:greetings), authors(:david).comments.find(:all, :order => 'comments.id').first
end end
def test_has_many_through_has_many_find_first def test_has_many_through_has_many_find_first
assert_equal comments(:greetings), authors(:david).comments.find(:first) assert_equal comments(:greetings), authors(:david).comments.find(:first, :order => 'comments.id')
end end
def test_has_many_through_has_many_find_conditions def test_has_many_through_has_many_find_conditions
@ -246,6 +246,14 @@ class AssociationsJoinModelTest < Test::Unit::TestCase
assert_equal comments(:more_greetings), authors(:david).comments.find(2) assert_equal comments(:more_greetings), authors(:david).comments.find(2)
end end
def test_eager_load_has_many_through_has_many
author = Author.find :first, :conditions => ['name = ?', 'David'], :include => :comments, :order => 'comments.id'
SpecialComment.new; VerySpecialComment.new
assert_no_queries do
assert_equal [1,2,3,5,6,7,8,9,10], author.comments.collect(&:id)
end
end
private private
# create dynamic Post models to allow different dependency options # create dynamic Post models to allow different dependency options
def find_post_with_dependency(post_id, association, association_name, dependency) def find_post_with_dependency(post_id, association, association_name, dependency)