Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed a bug when empty? was called on a grouped Relation that wasn't …

…loaded

[#5829 state:resolved]
  • Loading branch information...
commit 3a22e744162ce4d29f228e76d3e3ff355c93b88f 1 parent 4a26cd0
@pixeltrix pixeltrix authored
View
5 activerecord/lib/active_record/relation.rb
@@ -87,7 +87,10 @@ def size
# Returns true if there are no records.
def empty?
- loaded? ? @records.empty? : count.zero?
+ return @records.empty? if loaded?
+
+ c = count
+ c.respond_to?(:zero?) ? c.zero? : c.empty?
end
def any?
View
30 activerecord/test/cases/relations_test.rb
@@ -639,6 +639,36 @@ def test_count_complex_chained_relations
assert_equal expected, posts.count
end
+ def test_empty
+ posts = Post.scoped
+
+ assert_queries(1) { assert_equal false, posts.empty? }
+ assert ! posts.loaded?
+
+ no_posts = posts.where(:title => "")
+ assert_queries(1) { assert_equal true, no_posts.empty? }
+ assert ! no_posts.loaded?
+
+ best_posts = posts.where(:comments_count => 0)
+ best_posts.to_a # force load
+ assert_no_queries { assert_equal false, best_posts.empty? }
+ end
+
+ def test_empty_complex_chained_relations
+ posts = Post.select("comments_count").where("id is not null").group("author_id").where("comments_count > 0")
+ assert_queries(1) { assert_equal false, posts.empty? }
+ assert ! posts.loaded?
+
+ no_posts = posts.where(:title => "")
+ assert_queries(1) { assert_equal true, no_posts.empty? }
+ assert ! no_posts.loaded?
+
+ best_posts = posts.where(:comments_count => 0)
+ best_posts.to_a # force load
+ assert_no_queries { assert_equal true, best_posts.empty? }
+ assert best_posts.loaded?
+ end
+
def test_any
posts = Post.scoped
Please sign in to comment.
Something went wrong with that request. Please try again.