Skip to content
Browse files

Fix preloading of has_one through associations

[#903 state:resolved]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
1 parent f477676 commit fdeeeaea61bb34040f8da03afa9876171db4b17e @fcheung fcheung committed with jeremy Aug 25, 2008
View
8 activerecord/lib/active_record/association_preload.rb
@@ -51,9 +51,7 @@ def add_preloaded_records_to_collection(parent_records, reflection_name, associa
def add_preloaded_record_to_collection(parent_records, reflection_name, associated_record)
parent_records.each do |parent_record|
- association_proxy = parent_record.send(reflection_name)
- association_proxy.loaded
- association_proxy.target = associated_record
+ parent_record.send("set_#{reflection_name}_target", associated_record)
end
end
@@ -112,8 +110,8 @@ def preload_has_and_belongs_to_many_association(records, reflection, preload_opt
def preload_has_one_association(records, reflection, preload_options={})
id_to_record_map, ids = construct_id_map(records)
options = reflection.options
+ records.each {|record| record.send("set_#{reflection.name}_target", nil)}
if options[:through]
- records.each {|record| record.send(reflection.name) && record.send(reflection.name).loaded}
through_records = preload_through_records(records, reflection, options[:through])
through_reflection = reflections[options[:through]]
through_primary_key = through_reflection.primary_key_name
@@ -126,8 +124,6 @@ def preload_has_one_association(records, reflection, preload_options={})
end
end
else
- records.each {|record| record.send("set_#{reflection.name}_target", nil)}
-
set_association_single_records(id_to_record_map, reflection.name, find_associated_records(ids, reflection, preload_options), reflection.primary_key_name)
end
end
View
14 activerecord/test/cases/associations/has_one_through_associations_test.rb
@@ -44,19 +44,23 @@ def test_replacing_target_record_deletes_old_association
def test_has_one_through_polymorphic
assert_equal clubs(:moustache_club), @member.sponsor_club
end
-
+
def has_one_through_to_has_many
assert_equal 2, @member.fellow_members.size
end
-
+
def test_has_one_through_eager_loading
- members = Member.find(:all, :include => :club, :conditions => ["name = ?", "Groucho Marx"])
+ members = assert_queries(3) do #base table, through table, clubs table
+ Member.find(:all, :include => :club, :conditions => ["name = ?", "Groucho Marx"])
+ end
assert_equal 1, members.size
assert_not_nil assert_no_queries {members[0].club}
end
-
+
def test_has_one_through_eager_loading_through_polymorphic
- members = Member.find(:all, :include => :sponsor_club, :conditions => ["name = ?", "Groucho Marx"])
+ members = assert_queries(3) do #base table, through table, clubs table
+ Member.find(:all, :include => :sponsor_club, :conditions => ["name = ?", "Groucho Marx"])
+ end
assert_equal 1, members.size
assert_not_nil assert_no_queries {members[0].sponsor_club}
end

0 comments on commit fdeeeae

Please sign in to comment.
Something went wrong with that request. Please try again.