This project is archived and is in readonly mode.

#6425 new
Andrey Stikheev

has many through with order eager loading problem in rails 2.3.11

Reported by Andrey Stikheev | February 13th, 2011 @ 03:49 PM

A has_many through relationship with order raise a exception in rails >= 2.3.9 , in rails 2.3.8 it works fine.

To replicate:

Apply the following patch to rails source:


diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb
index b711719..52a9f85 100644
--- a/activerecord/test/cases/associations/eager_test.rb
+++ b/activerecord/test/cases/associations/eager_test.rb
@@ -328,6 +328,12 @@ class EagerAssociationTest < ActiveRecord::TestCase
     assert_equal authors(:david), assert_no_queries { posts_with_comments_and_author.first.author }
   end
 
+  def test_eager_with_has_many_through_with_model_with_include_and_with_order
+    assert_nothing_raised do
+      Author.find(authors(:david).id, :include => {:posts => :tags})
+    end
+  end
+
   def test_eager_with_has_many_through_a_belongs_to_association
     author = authors(:mary)
     post = Post.create!(:author => author, :title => "TITLE", :body => "BODY")
diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb
index cf69d04..daab631 100644
--- a/activerecord/test/models/post.rb
+++ b/activerecord/test/models/post.rb
@@ -46,7 +46,7 @@ class Post < ActiveRecord::Base
   has_and_belongs_to_many :special_categories, :join_table => "categories_posts", :association_foreign_key => 'category_id'
 
   has_many :taggings, :as => :taggable
-  has_many :tags, :through => :taggings do
+  has_many :tags, :through => :taggings, :order => 'tags.id' do
     def add_joins_and_select
       find :all, :select => 'tags.*, authors.id as author_id', :include => false,
         :joins => 'left outer join posts on taggings.taggable_id = posts.id left outer join authors on posts.author_id = authors.id'

Results of test:


sand:rails sand$ cd activerecord/
sand:activerecord sand$ rake  TEST=test/cases/associations/eager_test.rb 

...

  1) Failure:
test_eager_with_has_many_through_with_model_with_include_and_with_order(EagerAssociationTest)
    [./test/cases/associations/eager_test.rb:332:in `test_eager_with_has_many_through_with_model_with_include_and_with_order'
     ./test/cases/../../../activesupport/lib/active_support/testing/setup_and_teardown.rb:62:in `__send__'
     ./test/cases/../../../activesupport/lib/active_support/testing/setup_and_teardown.rb:62:in `run']:
Exception raised:
Class: <ActiveRecord::StatementInvalid>
Message: <"SQLite3::SQLException: no such column: tags.id: SELECT \"taggings\".* FROM \"taggings\" WHERE (\"taggings\".\"taggable_id\" IN (1,2,4,5,6) and \"taggings\".\"taggable_type\" = 'Post')  ORDER BY tags.id">
---Backtrace---
./test/cases/../../lib/active_record/connection_adapters/abstract_adapter.rb:227:in `log'
./test/cases/../../lib/active_record/connection_adapters/sqlite_adapter.rb:172:in `execute_without_query_record'
./test/cases/../../lib/active_record/connection_adapters/sqlite_adapter.rb:418:in `catch_schema_changes'
./test/cases/../../lib/active_record/connection_adapters/sqlite_adapter.rb:172:in `execute_without_query_record'
./test/cases/helper.rb:36:in `execute'
./test/cases/../../lib/active_record/connection_adapters/sqlite_adapter.rb:321:in `select'
./test/cases/../../lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache'
./test/cases/../../lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all'
./test/cases/../../lib/active_record/base.rb:665:in `find_by_sql'
./test/cases/../../lib/active_record/base.rb:1582:in `find_every'
./test/cases/../../lib/active_record/base.rb:619:in `find'
./test/cases/../../lib/active_record/association_preload.rb:379:in `find_associated_records'
./test/cases/../../lib/active_record/base.rb:2182:in `with_scope'
./test/cases/../../lib/active_record/base.rb:2190:in `with_exclusive_scope'
./test/cases/../../lib/active_record/association_preload.rb:378:in `find_associated_records'
./test/cases/../../lib/active_record/association_preload.rb:255:in `preload_has_many_association'
./test/cases/../../lib/active_record/association_preload.rb:120:in `send'
./test/cases/../../lib/active_record/association_preload.rb:120:in `preload_one_association'
./test/cases/../../../activesupport/lib/active_support/ordered_hash.rb:115:in `each'
./test/cases/../../../activesupport/lib/active_support/ordered_hash.rb:115:in `each'
./test/cases/../../lib/active_record/association_preload.rb:114:in `preload_one_association'
./test/cases/../../lib/active_record/association_preload.rb:91:in `preload_associations'
./test/cases/../../lib/active_record/association_preload.rb:289:in `preload_through_records'
./test/cases/../../lib/active_record/association_preload.rb:243:in `preload_has_many_association'
./test/cases/../../lib/active_record/association_preload.rb:120:in `send'
./test/cases/../../lib/active_record/association_preload.rb:120:in `preload_one_association'
./test/cases/../../../activesupport/lib/active_support/ordered_hash.rb:115:in `each'
./test/cases/../../../activesupport/lib/active_support/ordered_hash.rb:115:in `each'
./test/cases/../../lib/active_record/association_preload.rb:114:in `preload_one_association'
./test/cases/../../lib/active_record/association_preload.rb:91:in `preload_associations'
./test/cases/../../lib/active_record/association_preload.rb:99:in `preload_associations'
./test/cases/../../lib/active_record/association_preload.rb:93:in `each'
./test/cases/../../lib/active_record/association_preload.rb:93:in `preload_associations'
./test/cases/../../lib/active_record/association_preload.rb:90:in `preload_associations'
./test/cases/../../lib/active_record/association_preload.rb:90:in `each'
./test/cases/../../lib/active_record/association_preload.rb:90:in `preload_associations'
./test/cases/../../lib/active_record/base.rb:1584:in `find_every'
./test/cases/../../lib/active_record/base.rb:1617:in `find_one'
./test/cases/../../lib/active_record/base.rb:1603:in `find_from_ids'
./test/cases/../../lib/active_record/base.rb:620:in `find'
./test/cases/associations/eager_test.rb:333:in `test_eager_with_has_many_through_with_model_with_include_and_with_order'
./test/cases/associations/eager_test.rb:332:in `test_eager_with_has_many_through_with_model_with_include_and_with_order'
./test/cases/../../../activesupport/lib/active_support/testing/setup_and_teardown.rb:62:in `__send__'
./test/cases/../../../activesupport/lib/active_support/testing/setup_and_teardown.rb:62:in `run'
---------------

104 tests, 331 assertions, 1 failures, 0 errors

As I understood this problem appeared after the commit 0963774c0a6a58ba13ac (https://github.com/rails/rails/commit/0963774c0a6a58ba13ac0ff476352...)

No comments found

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>

People watching this ticket

Attachments

Pages