Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

DRY associations code and improve eager loading tests.

  • Loading branch information...
commit 802034ff5f1c3e3b576b664d5660e76c8f44909d 1 parent b28b54c
@lifo lifo authored
View
25 activerecord/lib/active_record/associations.rb
@@ -1451,9 +1451,6 @@ def construct_finder_sql_for_association_limiting(options, join_dependency)
join_dependency.joins_for_table_name(table)
}.flatten.compact.uniq
-
-
-
is_distinct = !options[:joins].blank? || include_eager_conditions?(options, tables_from_conditions) || include_eager_order?(options, tables_from_order)
sql = "SELECT "
if is_distinct
@@ -1507,29 +1504,17 @@ def selects_tables(options)
end
# Checks if the conditions reference a table other than the current model table
- def include_eager_conditions?(options,tables = nil)
- tables = conditions_tables(options)
- return false unless tables.any?
- tables.any? do |condition_table_name|
- condition_table_name != table_name
- end
+ def include_eager_conditions?(options, tables = nil)
+ ((tables || conditions_tables(options)) - [table_name]).any?
end
# Checks if the query order references a table other than the current model's table.
- def include_eager_order?(options,tables = nil)
- tables = order_tables(options)
- return false unless tables.any?
- tables.any? do |order_table_name|
- order_table_name != table_name
- end
+ def include_eager_order?(options, tables = nil)
+ ((tables || order_tables(options)) - [table_name]).any?
end
def include_eager_select?(options)
- selects = selects_tables(options)
- return false unless selects.any?
- selects.any? do |select|
- select != table_name
- end
+ (selects_tables(options) - [table_name]).any?
end
def references_eager_loaded_tables?(options)
View
5 activerecord/test/cases/finder_test.rb
@@ -9,6 +9,7 @@
require 'models/post'
require 'models/customer'
require 'models/job'
+require 'models/categorization'
class FinderTest < ActiveRecord::TestCase
fixtures :companies, :topics, :entrants, :developers, :developers_projects, :posts, :comments, :accounts, :authors, :customers
@@ -866,7 +867,9 @@ def test_find_with_order_on_included_associations_with_construct_finder_sql_for_
end
def test_with_limiting_with_custom_select
- assert_equal 3, Post.find(:all, :include => :author, :select => ' posts.*, authors.id as "author_id"', :limit => 3).size
+ posts = Post.find(:all, :include => :author, :select => ' posts.*, authors.id as "author_id"', :limit => 3)
+ assert_equal 3, posts.size
+ assert_equal [0, 1, 1], posts.map(&:author_id).sort
end
protected
Please sign in to comment.
Something went wrong with that request. Please try again.