aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/associations.rb18
-rw-r--r--activerecord/test/associations_go_eager_test.rb36
3 files changed, 51 insertions, 5 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 5aba184c82..8eab334fc6 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Ensure that Associations#include_eager_conditions? checks both scoped and explicit conditions [Rick]
+
* Associations#select_limited_ids_list adds the ORDER BY columns to the SELECT DISTINCT List for postgresql. [Rick]
* DRY up association collection reader method generation. [Marcel Molina Jr.]
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 88a2ad43ba..c0fdca3da9 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1192,16 +1192,24 @@ module ActiveRecord
add_limit!(sql, options, scope)
return sanitize_sql(sql)
end
-
+
+ # Checks if the conditions reference a table other than the current model table
def include_eager_conditions?(options)
- conditions = scope(:find, :conditions) || options[:conditions]
- return false unless conditions
- conditions = conditions.first if conditions.is_a?(Array)
- conditions.scan(/(\w+)\.\w+/).flatten.any? do |condition_table_name|
+ # look in both sets of conditions
+ conditions = [scope(:find, :conditions), options[:conditions]].inject([]) do |all, cond|
+ case cond
+ when nil then all
+ when Array then all << cond.first
+ else all << cond
+ end
+ end
+ return false unless conditions.any?
+ conditions.join(' ').scan(/(\w+)\.\w+/).flatten.any? do |condition_table_name|
condition_table_name != table_name
end
end
+ # Checks if the query order references a table other than the current model's table.
def include_eager_order?(options)
order = options[:order]
return false unless order
diff --git a/activerecord/test/associations_go_eager_test.rb b/activerecord/test/associations_go_eager_test.rb
index 324e8eb231..d2ae4e0dd9 100644
--- a/activerecord/test/associations_go_eager_test.rb
+++ b/activerecord/test/associations_go_eager_test.rb
@@ -179,6 +179,42 @@ class EagerAssociationTest < Test::Unit::TestCase
assert_equal count, posts.size
end
+ def test_eager_with_has_many_and_limit_and_scoped_conditions_on_the_eagers
+ posts = nil
+ Post.with_scope(:find => {
+ :include => :comments,
+ :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'"
+ }) do
+ posts = authors(:david).posts.find(:all, :limit => 2)
+ assert_equal 2, posts.size
+ end
+
+ Post.with_scope(:find => {
+ :include => [ :comments, :author ],
+ :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')"
+ }) do
+ count = Post.count(:limit => 2)
+ assert_equal count, posts.size
+ end
+ end
+
+ def test_eager_with_has_many_and_limit_and_scoped_and_explicit_conditions_on_the_eagers
+ Post.with_scope(:find => { :conditions => "1=1" }) do
+ posts = authors(:david).posts.find(:all,
+ :include => :comments,
+ :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
+ :limit => 2
+ )
+ assert_equal 2, posts.size
+
+ count = Post.count(
+ :include => [ :comments, :author ],
+ :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')",
+ :limit => 2
+ )
+ assert_equal count, posts.size
+ end
+ end
def test_eager_association_loading_with_habtm
posts = Post.find(:all, :include => :categories, :order => "posts.id")
assert_equal 2, posts[0].categories.size