This project is archived and is in readonly mode.

#2362 ✓committed
Roy W. Black

[patch] has many through eager loading problem

Reported by Roy W. Black | March 27th, 2009 @ 06:34 PM | in 3.0.2

A has_many through relationship with conditions will load as if the conditions don't exist when eager loading.

To replicate:

Apply the following patch to rails source. It adds a has_many through (HMT) relationship with conditions to the post model and a test that shows the wrong thing is loaded when eager loading is used

diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb
index 4072381..e902637 100644
--- a/activerecord/test/cases/associations/eager_test.rb
+++ b/activerecord/test/cases/associations/eager_test.rb
@@ -352,6 +352,12 @@ class EagerAssociationTest < ActiveRecord::TestCase
     end
   end

+  def test_eager_with_has_many_through_with_conditions_join_model_with_include
+    post_tags = Post.find(posts(:welcome).id).misc_tags
+    eager_post_tags = Post.find(posts(:welcome).id, :include => :misc_tags).misc_tags
+    assert_equal post_tags, eager_post_tags
+  end
+
   def test_eager_with_has_many_and_limit
     posts = Post.find(:all, :order => 'posts.id asc', :include => [ :author, :comments ], :limit => 2)
     assert_equal 2, posts.size
diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb
index 374e536..1691666 100644
--- a/activerecord/test/models/post.rb
+++ b/activerecord/test/models/post.rb
@@ -52,6 +52,7 @@ class Post < ActiveRecord::Base
     end
   end

+  has_many :misc_tags, :through => :taggings, :source => :tag, :conditions => "tags.name = 'Misc'"
   has_many :funky_tags, :through => :taggings, :source => :tag
   has_many :super_tags, :through => :taggings
   has_one :tagging, :as => :taggable

Results of test

C:\local\rails\activerecord
14:27:03.48>rake test_sqlite3 TEST=test\cases\associations\eager_test.rb
(in C:/local/rails/activerecord)
c:/local/ruby/bin/ruby -I"C:/local/rails/activerecord/lib" -I"C:/local/rails/activerecord/test" -I"C:/local/rails/activerecord/test/
connections/native_sqlite3" "c:/local/ruby/lib/ruby/gems/1.8/gems/rake-0.8.4/lib/rake/rake_test_loader.rb" "test\cases\associations\
eager_test.rb"
Using native SQLite3
Loaded suite c:/local/ruby/lib/ruby/gems/1.8/gems/rake-0.8.4/lib/rake/rake_test_loader
Started
.........................................................F..........................................
Finished in 2.297 seconds.

  1) Failure:
test_eager_with_has_many_through_with_conditions_join_model_with_include(EagerAssociationTest)
    [./test\cases\associations\eager_test.rb:358:in `test_eager_with_has_many_through_with_conditions_join_model_with_include'
     C:/local/rails/activerecord/test/cases/../../../activesupport/lib/active_support/testing/setup_and_teardown.rb:62:in `__send__'

     C:/local/rails/activerecord/test/cases/../../../activesupport/lib/active_support/testing/setup_and_teardown.rb:62:in `run']:
<[]> expected but was
<[#<Tag id: 1, name: "General", taggings_count: 0>]>.

100 tests, 322 assertions, 1 failures, 0 errors
rake aborted!
Command failed with status (1): [c:/local/ruby/bin/ruby -I"C:/local/rails/a...]

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages