From 32e44bd4e695bf8f0d75d7c65a97df176d0369b7 Mon Sep 17 00:00:00 2001 From: Tiago Macedo Date: Sun, 8 Jun 2008 17:00:56 +0100 Subject: [PATCH] fixing conditions and order on join tables with limited eager loading --- activerecord/lib/active_record/associations.rb | 4 +++- activerecord/test/cases/associations/eager_test.rb | 13 ++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index a3d1bbb..8d102a9 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1638,7 +1638,9 @@ module ActiveRecord end def join_for_table_name(table_name) - @joins.select{|j|j.aliased_table_name == table_name.gsub(/^\"(.*)\"$/){$1} }.first rescue nil + join = (@joins.select{|j|j.aliased_table_name == table_name.gsub(/^\"(.*)\"$/){$1} }.first) rescue nil + return join unless join.nil? + @joins.select{|j|j.is_a?(JoinAssociation) && j.aliased_join_table_name == table_name.gsub(/^\"(.*)\"$/){$1} }.first rescue nil end def joins_for_table_name(table_name) diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 3a3358e..0d4ef0b 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -14,11 +14,14 @@ require 'models/job' require 'models/subscriber' require 'models/subscription' require 'models/book' +require 'models/developer' +require 'models/project' class EagerAssociationTest < ActiveRecord::TestCase fixtures :posts, :comments, :authors, :categories, :categories_posts, :companies, :accounts, :tags, :taggings, :people, :readers, - :owners, :pets, :author_favorites, :jobs, :references, :subscribers, :subscriptions, :books + :owners, :pets, :author_favorites, :jobs, :references, :subscribers, :subscriptions, :books, + :developers, :projects def test_loading_with_one_association posts = Post.find(:all, :include => :comments) @@ -609,4 +612,12 @@ class EagerAssociationTest < ActiveRecord::TestCase Comment.find :all, :include => :post end end + + def test_conditions_on_join_table_with_include_and_limit + assert_equal 3, Developer.find(:all, :include => 'projects', :conditions => 'developers_projects.access_level = 1', :limit => 5).size + end + + def test_order_on_join_table_with_include_and_limit + assert_equal 5, Developer.find(:all, :include => 'projects', :order => 'developers_projects.joined_on DESC', :limit => 5).size + end end -- 1.5.5.3