From 338aa3177c795ff71b400ef62f64b2364344a817 Mon Sep 17 00:00:00 2001 From: Bogdan Gusiev Date: Sat, 26 Jun 2010 21:23:00 +0300 Subject: [PATCH] has_many through belongs_to association bug fix [#4887 state:resolved] --- .../associations/has_many_through_association.rb | 8 ++++++++ .../has_many_through_associations_test.rb | 4 ++++ 2 files changed, 12 insertions(+), 0 deletions(-) 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 17f8507..f57791f 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -110,6 +110,14 @@ module ActiveRecord def we_can_set_the_inverse_on_this?(record) false end + + def construct_scope + if target_reflection_has_associated_record? + super + else + {:find => {:conditions => "1 != 1"}} + end + end end end end diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index e4dd810..d2679c2 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -351,7 +351,10 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase def test_has_many_association_through_a_belongs_to_association_where_the_association_doesnt_exist author = authors(:mary) post = Post.create!(:title => "TITLE", :body => "BODY") + AuthorFavorite.create! assert_equal [], post.author_favorites + assert_equal [], post.author_favorites.all + assert_equal 0, post.author_favorites.count end def test_has_many_association_through_a_belongs_to_association @@ -363,6 +366,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert_equal post.author.author_favorites, post.author_favorites end + def test_has_many_association_through_a_has_many_association_with_nonstandard_primary_keys assert_equal 1, owners(:blackbeard).toys.count end -- 1.6.3.3