From 2cfd6c00a158384e45c2f7c7e27c9bc42b23d4c7 Mon Sep 17 00:00:00 2001 From: Will Bryant Date: Thu, 13 Aug 2009 12:38:20 +1200 Subject: [PATCH] Verify that has_one :through preload respects the :conditions [#2976 state:resolved] --- .../has_one_through_associations_test.rb | 12 ++++++++++++ activerecord/test/models/member.rb | 3 ++- 2 files changed, 14 insertions(+), 1 deletions(-) diff --git a/activerecord/test/cases/associations/has_one_through_associations_test.rb b/activerecord/test/cases/associations/has_one_through_associations_test.rb index 93a4f49..7982e7f 100644 --- a/activerecord/test/cases/associations/has_one_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_through_associations_test.rb @@ -88,6 +88,18 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase assert_not_nil assert_no_queries {members[0].sponsor_club} end + def test_has_one_through_with_conditions_eager_loading + # conditions on the through table + assert_equal clubs(:moustache_club), Member.find(@member.id, :include => :favourite_club).favourite_club + memberships(:membership_of_favourite_club).update_attribute(:favourite, false) + assert_equal nil, Member.find(@member.id, :include => :favourite_club).favourite_club + + # conditions on the source table + assert_equal clubs(:moustache_club), Member.find(@member.id, :include => :hairy_club).hairy_club + clubs(:moustache_club).update_attribute(:name, "Association of Clean-Shaven Persons") + assert_equal nil, Member.find(@member.id, :include => :hairy_club).hairy_club + end + def test_has_one_through_polymorphic_with_source_type assert_equal members(:groucho), clubs(:moustache_club).sponsored_member end diff --git a/activerecord/test/models/member.rb b/activerecord/test/models/member.rb index 255fb56..31e25e8 100644 --- a/activerecord/test/models/member.rb +++ b/activerecord/test/models/member.rb @@ -4,9 +4,10 @@ class Member < ActiveRecord::Base has_many :fellow_members, :through => :club, :source => :members has_one :club, :through => :current_membership has_one :favourite_club, :through => :memberships, :conditions => ["memberships.favourite = ?", true], :source => :club + has_one :hairy_club, :through => :memberships, :conditions => {:clubs => {:name => "Moustache and Eyebrow Fancier Club"}}, :source => :club has_one :sponsor, :as => :sponsorable has_one :sponsor_club, :through => :sponsor has_one :member_detail has_one :organization, :through => :member_detail belongs_to :member_type -end \ No newline at end of file +end -- 1.7.1