Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 9075

Show
Ignore:
Timestamp:
03/22/08 02:20:37 (4 months ago)
Author:
bitsweat
Message:

has_one :through supports :source_type. Fix up some tests. References #4756.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/lib/active_record/association_preload.rb

    r9067 r9075  
    166166          records.compact.each do |record| 
    167167            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 
    170175          end 
    171176          through_records = through_records.flatten 
  • trunk/activerecord/lib/active_record/associations.rb

    r9068 r9075  
    13271327        def create_has_one_through_reflection(association_id, options) 
    13281328          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 
    13301330          ) 
    13311331          create_reflection(:has_one, association_id, options, self) 
  • trunk/activerecord/test/cases/associations_test.rb

    r9068 r9075  
    527527   
    528528  def test_has_one_through_eager_loading 
    529     members = Member.find(:all, :include => :club
    530     assert_equal 2, members.size 
     529    members = Member.find(:all, :include => :club, :conditions => ["name = ?", "Groucho Marx"]
     530    assert_equal 1, members.size 
    531531    assert_not_nil assert_no_queries {members[0].club} 
    532532  end 
    533533   
    534534  def test_has_one_through_eager_loading_through_polymorphic 
    535     members = Member.find(:all, :include => :sponsor_club
    536     assert_equal 2, members.size 
     535    members = Member.find(:all, :include => :sponsor_club, :conditions => ["name = ?", "Groucho Marx"]
     536    assert_equal 1, members.size 
    537537    assert_not_nil assert_no_queries {members[0].sponsor_club}     
    538538  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 
    539550end 
    540551 
  • trunk/activerecord/test/fixtures/sponsors.yml

    r9067 r9075  
    22  sponsor_club: moustache_club 
    33  sponsorable: groucho (Member) 
     4boring_club_sponsor_for_groucho: 
     5  sponsor_club: boring_club 
     6  sponsorable: some_other_guy (Member) 
     7crazy_club_sponsor_for_groucho: 
     8  sponsor_club: crazy_club 
     9  sponsorable: some_other_guy (Member) 
  • trunk/activerecord/test/models/club.rb

    r9067 r9075  
    33  has_many :members, :through => :memberships 
    44  has_many :current_memberships 
    5   has_many :sponsors 
     5  has_one :sponsor 
     6  has_one :sponsored_member, :through => :sponsor, :source => :sponsorable, :source_type => "Member" 
    67end