diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2008-03-22 02:20:37 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-03-22 02:20:37 +0000 |
commit | 5c1be2812ddc81a3ac5b65c8a461a9622e41b31c (patch) | |
tree | b97565500852ab3994059b455725225d3d6bd2f0 | |
parent | f921a96e8506ca5d376a83f06b45ffd8df737fa7 (diff) | |
download | rails-5c1be2812ddc81a3ac5b65c8a461a9622e41b31c.tar.gz rails-5c1be2812ddc81a3ac5b65c8a461a9622e41b31c.tar.bz2 rails-5c1be2812ddc81a3ac5b65c8a461a9622e41b31c.zip |
has_one :through supports :source_type. Fix up some tests. References #4756.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9075 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | activerecord/lib/active_record/association_preload.rb | 9 | ||||
-rwxr-xr-x | activerecord/lib/active_record/associations.rb | 2 | ||||
-rwxr-xr-x | activerecord/test/cases/associations_test.rb | 19 | ||||
-rw-r--r-- | activerecord/test/fixtures/sponsors.yml | 8 | ||||
-rw-r--r-- | activerecord/test/models/club.rb | 3 |
5 files changed, 32 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb index 0c31c85bb0..ad9b9d6488 100644 --- a/activerecord/lib/active_record/association_preload.rb +++ b/activerecord/lib/active_record/association_preload.rb @@ -165,8 +165,13 @@ module ActiveRecord through_records = [] records.compact.each do |record| proxy = record.send(through_association) - through_records << proxy.target - proxy.reset + + if proxy.respond_to?(:target) + through_records << proxy.target + proxy.reset + else # this is a has_one :through reflection + through_records << proxy if proxy + end end through_records = through_records.flatten else diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index aa68a42c44..068f2f9de2 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1326,7 +1326,7 @@ module ActiveRecord def create_has_one_through_reflection(association_id, options) options.assert_valid_keys( - :class_name, :foreign_key, :remote, :conditions, :order, :include, :dependent, :counter_cache, :extend, :as, :through, :source + :class_name, :foreign_key, :remote, :conditions, :order, :include, :dependent, :counter_cache, :extend, :as, :through, :source, :source_type ) create_reflection(:has_one, association_id, options, self) end diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index 8ae9e5630c..48c618b2c1 100755 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -526,16 +526,27 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase end def test_has_one_through_eager_loading - members = Member.find(:all, :include => :club) - assert_equal 2, members.size + members = Member.find(:all, :include => :club, :conditions => ["name = ?", "Groucho Marx"]) + 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) - assert_equal 2, members.size + members = Member.find(:all, :include => :sponsor_club, :conditions => ["name = ?", "Groucho Marx"]) + assert_equal 1, members.size assert_not_nil assert_no_queries {members[0].sponsor_club} end + + def test_has_one_through_polymorphic_with_source_type + assert_equal members(:groucho), clubs(:moustache_club).sponsored_member + end + + def test_eager_has_one_through_polymorphic_with_source_type + clubs = Club.find(:all, :include => :sponsored_member, :conditions => ["name = ?","Moustache and Eyebrow Fancier Club"]) + # Only the eyebrow fanciers club has a sponsored_member + assert_not_nil assert_no_queries {clubs[0].sponsored_member} + end + end class HasManyAssociationsTest < ActiveRecord::TestCase diff --git a/activerecord/test/fixtures/sponsors.yml b/activerecord/test/fixtures/sponsors.yml index c116df0534..42df8957d1 100644 --- a/activerecord/test/fixtures/sponsors.yml +++ b/activerecord/test/fixtures/sponsors.yml @@ -1,3 +1,9 @@ moustache_club_sponsor_for_groucho: sponsor_club: moustache_club - sponsorable: groucho (Member)
\ No newline at end of file + sponsorable: groucho (Member) +boring_club_sponsor_for_groucho: + sponsor_club: boring_club + sponsorable: some_other_guy (Member) +crazy_club_sponsor_for_groucho: + sponsor_club: crazy_club + sponsorable: some_other_guy (Member)
\ No newline at end of file diff --git a/activerecord/test/models/club.rb b/activerecord/test/models/club.rb index 171445df3e..3ddb691dfb 100644 --- a/activerecord/test/models/club.rb +++ b/activerecord/test/models/club.rb @@ -2,5 +2,6 @@ class Club < ActiveRecord::Base has_many :memberships has_many :members, :through => :memberships has_many :current_memberships - has_many :sponsors + has_one :sponsor + has_one :sponsored_member, :through => :sponsor, :source => :sponsorable, :source_type => "Member" end
\ No newline at end of file |