Changeset 9075
- Timestamp:
- 03/22/08 02:20:37 (4 months ago)
- Files:
-
- trunk/activerecord/lib/active_record/association_preload.rb (modified) (1 diff)
- trunk/activerecord/lib/active_record/associations.rb (modified) (1 diff)
- trunk/activerecord/test/cases/associations_test.rb (modified) (1 diff)
- trunk/activerecord/test/fixtures/sponsors.yml (modified) (1 diff)
- trunk/activerecord/test/models/club.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/lib/active_record/association_preload.rb
r9067 r9075 166 166 records.compact.each do |record| 167 167 proxy = record.send(through_association) 168 through_records << proxy.target 169 proxy.reset 168 169 if proxy.respond_to?(:target) 170 through_records << proxy.target 171 proxy.reset 172 else # this is a has_one :through reflection 173 through_records << proxy if proxy 174 end 170 175 end 171 176 through_records = through_records.flatten trunk/activerecord/lib/active_record/associations.rb
r9068 r9075 1327 1327 def create_has_one_through_reflection(association_id, options) 1328 1328 options.assert_valid_keys( 1329 :class_name, :foreign_key, :remote, :conditions, :order, :include, :dependent, :counter_cache, :extend, :as, :through, :source 1329 :class_name, :foreign_key, :remote, :conditions, :order, :include, :dependent, :counter_cache, :extend, :as, :through, :source, :source_type 1330 1330 ) 1331 1331 create_reflection(:has_one, association_id, options, self) trunk/activerecord/test/cases/associations_test.rb
r9068 r9075 527 527 528 528 def test_has_one_through_eager_loading 529 members = Member.find(:all, :include => :club )530 assert_equal 2, members.size529 members = Member.find(:all, :include => :club, :conditions => ["name = ?", "Groucho Marx"]) 530 assert_equal 1, members.size 531 531 assert_not_nil assert_no_queries {members[0].club} 532 532 end 533 533 534 534 def test_has_one_through_eager_loading_through_polymorphic 535 members = Member.find(:all, :include => :sponsor_club )536 assert_equal 2, members.size535 members = Member.find(:all, :include => :sponsor_club, :conditions => ["name = ?", "Groucho Marx"]) 536 assert_equal 1, members.size 537 537 assert_not_nil assert_no_queries {members[0].sponsor_club} 538 538 end 539 540 def test_has_one_through_polymorphic_with_source_type 541 assert_equal members(:groucho), clubs(:moustache_club).sponsored_member 542 end 543 544 def test_eager_has_one_through_polymorphic_with_source_type 545 clubs = Club.find(:all, :include => :sponsored_member, :conditions => ["name = ?","Moustache and Eyebrow Fancier Club"]) 546 # Only the eyebrow fanciers club has a sponsored_member 547 assert_not_nil assert_no_queries {clubs[0].sponsored_member} 548 end 549 539 550 end 540 551 trunk/activerecord/test/fixtures/sponsors.yml
r9067 r9075 2 2 sponsor_club: moustache_club 3 3 sponsorable: groucho (Member) 4 boring_club_sponsor_for_groucho: 5 sponsor_club: boring_club 6 sponsorable: some_other_guy (Member) 7 crazy_club_sponsor_for_groucho: 8 sponsor_club: crazy_club 9 sponsorable: some_other_guy (Member) trunk/activerecord/test/models/club.rb
r9067 r9075 3 3 has_many :members, :through => :memberships 4 4 has_many :current_memberships 5 has_many :sponsors 5 has_one :sponsor 6 has_one :sponsored_member, :through => :sponsor, :source => :sponsorable, :source_type => "Member" 6 7 end