From 3104291322b4f1834d342eef740c23427f36384d Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Sun, 11 Nov 2007 01:03:43 +0000 Subject: [PATCH] Fix HasManyThrough Association so it uses :conditions on the HasMany Association. Closes #9729 [danger] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8128 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ .../associations/has_many_through_association.rb | 3 ++- activerecord/test/associations/join_model_test.rb | 6 ++++++ activerecord/test/fixtures/author.rb | 1 + activerecord/test/fixtures/post.rb | 1 + 5 files changed, 12 insertions(+), 1 deletion(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 3c2cd8a0fd..54fa7ac60c 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fix HasManyThrough Association so it uses :conditions on the HasMany Association. Closes #9729 [danger] + * Ensure that column names are quoted. Closes #10134 [wesley.moxam] * Smattering of grammatical fixes to documentation. Closes #10083 [BobSilva] diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index 5ff0e3d0d7..8bff88e839 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -262,8 +262,9 @@ def conditions @conditions ||= [ (interpolate_sql(@reflection.klass.send(:sanitize_sql, @reflection.options[:conditions])) if @reflection.options[:conditions]), (interpolate_sql(@reflection.active_record.send(:sanitize_sql, @reflection.through_reflection.options[:conditions])) if @reflection.through_reflection.options[:conditions]), + (interpolate_sql(@reflection.active_record.send(:sanitize_sql, @reflection.source_reflection.options[:conditions])) if @reflection.source_reflection.options[:conditions]), ("#{@reflection.through_reflection.table_name}.#{@reflection.through_reflection.klass.inheritance_column} = #{@reflection.klass.quote_value(@reflection.through_reflection.klass.name.demodulize)}" unless @reflection.through_reflection.klass.descends_from_active_record?) - ].compact.collect { |condition| "(#{condition})" }.join(' AND ') unless (!@reflection.options[:conditions] && !@reflection.through_reflection.options[:conditions] && @reflection.through_reflection.klass.descends_from_active_record?) + ].compact.collect { |condition| "(#{condition})" }.join(' AND ') unless (!@reflection.options[:conditions] && !@reflection.through_reflection.options[:conditions] && !@reflection.source_reflection.options[:conditions] && @reflection.through_reflection.klass.descends_from_active_record?) end alias_method :sql_conditions, :conditions diff --git a/activerecord/test/associations/join_model_test.rb b/activerecord/test/associations/join_model_test.rb index 0e146f74c4..631f7034f4 100644 --- a/activerecord/test/associations/join_model_test.rb +++ b/activerecord/test/associations/join_model_test.rb @@ -410,6 +410,12 @@ def test_add_to_self_referential_has_many_through authors(:david).author_favorites.create :favorite_author => new_author assert_equal new_author, authors(:david).reload.favorite_authors.first end + + def test_has_many_through_uses_conditions_specified_on_the_has_many_association + author = Author.find(:first) + assert !author.comments.blank? + assert author.nonexistant_comments.blank? + end def test_has_many_through_uses_correct_attributes assert_nil posts(:thinking).tags.find_by_name("General").attributes["tag_id"] diff --git a/activerecord/test/fixtures/author.rb b/activerecord/test/fixtures/author.rb index 5ef0c6cfe9..979c72ec63 100644 --- a/activerecord/test/fixtures/author.rb +++ b/activerecord/test/fixtures/author.rb @@ -22,6 +22,7 @@ def testing_proxy_target has_many :special_nonexistant_posts, :class_name => "SpecialPost", :conditions => "posts.body = 'nonexistant'" has_many :special_nonexistant_post_comments, :through => :special_nonexistant_posts, :source => :comments, :conditions => "comments.post_id = 0" + has_many :nonexistant_comments, :through => :posts has_many :hello_posts, :class_name => "Post", :conditions => "posts.body = 'hello'" has_many :hello_post_comments, :through => :hello_posts, :source => :comments diff --git a/activerecord/test/fixtures/post.rb b/activerecord/test/fixtures/post.rb index 44ac3c1053..4627da5206 100644 --- a/activerecord/test/fixtures/post.rb +++ b/activerecord/test/fixtures/post.rb @@ -16,6 +16,7 @@ def find_most_recent has_one :very_special_comment has_one :very_special_comment_with_post, :class_name => "VerySpecialComment", :include => :post has_many :special_comments + has_many :nonexistant_comments, :class_name => 'Comment', :conditions => 'comments.id < 0' has_and_belongs_to_many :categories has_and_belongs_to_many :special_categories, :join_table => "categories_posts", :association_foreign_key => 'category_id' -- GitLab