Skip to content

Commit

Permalink
Fix conditions and order on join tables with limited eager loading. [#…
Browse files Browse the repository at this point in the history
…372 state:resolved]
  • Loading branch information
Tiago Macedo authored and jeremy committed Jun 8, 2008
1 parent 475527c commit 0aedc7a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 3 additions & 1 deletion activerecord/lib/active_record/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1638,7 +1638,9 @@ def remove_duplicate_results!(base, records, associations)
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)
Expand Down
13 changes: 12 additions & 1 deletion activerecord/test/cases/associations/eager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
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)
Expand Down Expand Up @@ -609,4 +612,12 @@ def test_load_with_sti_sharing_association
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

0 comments on commit 0aedc7a

Please sign in to comment.