Skip to content

Commit

Permalink
fix eager loading with dynamic finders
Browse files Browse the repository at this point in the history
  • Loading branch information
bkeepers authored and technoweenie committed Jun 19, 2008
1 parent 8c0ce21 commit 7827b07
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/associations.rb
Expand Up @@ -1514,7 +1514,7 @@ def conditions_tables(options)
end

def order_tables(options)
order = options[:order]
order = [options[:order], scope(:find, :order) ].join(", ")
return [] unless order && order.is_a?(String)
order.scan(/([\.\w]+).?\./).flatten
end
Expand Down
Expand Up @@ -87,6 +87,7 @@ def construct_scope
:joins => @join_sql,
:readonly => false,
:order => @reflection.options[:order],
:include => @reflection.options[:include],
:limit => @reflection.options[:limit] } }
end

Expand Down
Expand Up @@ -100,7 +100,7 @@ def construct_scope
create_scoping = {}
set_belongs_to_association_for(create_scoping)
{
:find => { :conditions => @finder_sql, :readonly => false, :order => @reflection.options[:order], :limit => @reflection.options[:limit] },
:find => { :conditions => @finder_sql, :readonly => false, :order => @reflection.options[:order], :limit => @reflection.options[:limit], :include => @reflection.options[:include]},
:create => create_scoping
}
end
Expand Down
Expand Up @@ -681,4 +681,10 @@ def test_symbols_as_keys
assert_equal developer, project.developers.find(:first)
assert_equal project, developer.projects.find(:first)
end

def test_dynamic_find_should_respect_association_include
# SQL error in sort clause if :include is not included
# due to Unknown column 'authors.id'
assert Category.find(1).posts_with_authors_sorted_by_author_id.find_by_title('Welcome to the weblog')
end
end
Expand Up @@ -187,4 +187,10 @@ def test_association_callback_ordering
post.people_with_callbacks.clear
assert_equal (%w(Michael David Julian Roger) * 2).sort, log.last(8).collect(&:last).sort
end

def test_dynamic_find_should_respect_association_include
# SQL error in sort clause if :include is not included
# due to Unknown column 'comments.id'
assert Person.find(1).posts_with_comments_sorted_by_comment_id.find_by_title('Welcome to the weblog')
end
end
1 change: 1 addition & 0 deletions activerecord/test/models/author.rb
@@ -1,6 +1,7 @@
class Author < ActiveRecord::Base
has_many :posts
has_many :posts_with_comments, :include => :comments, :class_name => "Post"
has_many :posts_with_comments_sorted_by_comment_id, :include => :comments, :class_name => "Post", :order => 'comments.id'
has_many :posts_with_categories, :include => :categories, :class_name => "Post"
has_many :posts_with_comments_and_categories, :include => [ :comments, :categories ], :order => "posts.id", :class_name => "Post"
has_many :posts_containing_the_letter_a, :class_name => "Post"
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/models/category.rb
Expand Up @@ -2,6 +2,7 @@ class Category < ActiveRecord::Base
has_and_belongs_to_many :posts
has_and_belongs_to_many :special_posts, :class_name => "Post"
has_and_belongs_to_many :other_posts, :class_name => "Post"
has_and_belongs_to_many :posts_with_authors_sorted_by_author_id, :class_name => "Post", :include => :authors, :order => "authors.id"

has_and_belongs_to_many(:select_testing_posts,
:class_name => 'Post',
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/models/person.rb
Expand Up @@ -6,5 +6,5 @@ class Person < ActiveRecord::Base
has_many :references
has_many :jobs, :through => :references
has_one :favourite_reference, :class_name => 'Reference', :conditions => ['favourite=?', true]

has_many :posts_with_comments_sorted_by_comment_id, :through => :readers, :source => :post, :include => :comments, :order => 'comments.id'
end

0 comments on commit 7827b07

Please sign in to comment.