Permalink
Browse files

applying to stable: Ensure that Associations#include_eager_conditions…

…? checks both scoped and explicit conditions [Rick]

git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/stable@4234 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 982d187 commit a9ad634dbc7b2810dffa98079b0d7193be7a4f4f @technoweenie technoweenie committed Apr 19, 2006
View
@@ -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]
@@ -1187,16 +1187,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
@@ -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

0 comments on commit a9ad634

Please sign in to comment.