Skip to content

Commit

Permalink
Implement old-skool eagerloading for has_one :through
Browse files Browse the repository at this point in the history
Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information
fcheung authored and jeremy committed Aug 26, 2008
1 parent fdeeeae commit bff0f5f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/associations.rb
Expand Up @@ -1815,7 +1815,7 @@ def initialize(reflection, join_dependency, parent = nil)
@aliased_join_table_name = aliased_table_name_for(reflection.options[:join_table], "_join")
end

if reflection.macro == :has_many && reflection.options[:through]
if [:has_many, :has_one].include?(reflection.macro) && reflection.options[:through]
@aliased_join_table_name = aliased_table_name_for(reflection.through_reflection.klass.table_name, "_join")
end
end
Expand All @@ -1839,7 +1839,7 @@ def association_join
]
when :has_many, :has_one
case
when reflection.macro == :has_many && reflection.options[:through]
when reflection.options[:through]
through_conditions = through_reflection.options[:conditions] ? "AND #{interpolate_sql(sanitize_sql(through_reflection.options[:conditions]))}" : ''

jt_foreign_key = jt_as_extra = jt_source_extra = jt_sti_extra = nil
Expand Down
Expand Up @@ -75,4 +75,20 @@ def test_eager_has_one_through_polymorphic_with_source_type
assert_not_nil assert_no_queries {clubs[0].sponsored_member}
end

def test_has_one_through_nonpreload_eagerloading
members = assert_queries(1) do
Member.find(:all, :include => :club, :conditions => ["members.name = ?", "Groucho Marx"], :order => 'clubs.name') #force fallback
end
assert_equal 1, members.size
assert_not_nil assert_no_queries {members[0].club}
end

def test_has_one_through_nonpreload_eager_loading_through_polymorphic
members = assert_queries(1) do
Member.find(:all, :include => :sponsor_club, :conditions => ["members.name = ?", "Groucho Marx"], :order => 'clubs.name') #force fallback
end
assert_equal 1, members.size
assert_not_nil assert_no_queries {members[0].sponsor_club}
end

end

0 comments on commit bff0f5f

Please sign in to comment.