Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Ensure that Associations#include_eager_conditions? checks both scoped…

… and explicit conditions [Rick]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4232 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 2a2afca0955fa43a0796d727399f30a2c09bfc5d 1 parent 5ea76fa
@technoweenie technoweenie authored
View
2  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.]
View
18 activerecord/lib/active_record/associations.rb
@@ -1192,16 +1192,24 @@ def construct_finder_sql_for_association_limiting(options, join_dependency)
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
View
36 activerecord/test/associations_go_eager_test.rb
@@ -179,6 +179,42 @@ def test_eager_with_has_many_and_limit_and_conditions_on_the_eagers
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
Please sign in to comment.
Something went wrong with that request. Please try again.