Guest User

Untitled

a guest
Feb 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. From 7f49582ac214d20258dccd222e360350591e1cce Mon Sep 17 00:00:00 2001
  2. From: =?utf-8?q?Tarmo=20T=C3=A4nav?= <tarmo@itech.ee>
  3. Date: Wed, 23 Jul 2008 14:57:42 +0300
  4. Subject: [PATCH] has_many association and named_scope empty?() uses exists?({}) instead of counting.
  5.  
  6. This way the database only has to find one match using an index
  7. instead of counting all the matches. If the association or scope
  8. is already loaded or the association has a custom counter_sql
  9. size check is used instead.
  10. ---
  11. .../associations/association_collection.rb | 6 +++++-
  12. activerecord/lib/active_record/named_scope.rb | 2 +-
  13. .../associations/has_many_associations_test.rb | 16 ++++++++++++++++
  14. activerecord/test/cases/named_scope_test.rb | 11 +++++++++++
  15. 4 files changed, 33 insertions(+), 2 deletions(-)
  16.  
  17. diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb
  18. index a28be9e..9c44eb9 100644
  19. --- a/activerecord/lib/active_record/associations/association_collection.rb
  20. +++ b/activerecord/lib/active_record/associations/association_collection.rb
  21. @@ -206,7 +206,11 @@ module ActiveRecord
  22. end
  23.  
  24. def empty?
  25. - size.zero?
  26. + if loaded? || @reflection.options[:counter_sql]
  27. + size.zero?
  28. + else
  29. + !exists?({})
  30. + end
  31. end
  32.  
  33. def any?
  34. diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb
  35. index d5a1c5f..ab842b4 100644
  36. --- a/activerecord/lib/active_record/named_scope.rb
  37. +++ b/activerecord/lib/active_record/named_scope.rb
  38. @@ -137,7 +137,7 @@ module ActiveRecord
  39. end
  40.  
  41. def empty?
  42. - @found ? @found.empty? : count.zero?
  43. + @found ? @found.empty? : !exists?({})
  44. end
  45.  
  46. def any?
  47. diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
  48. index f8b8b1f..899e20d 100644
  49. --- a/activerecord/test/cases/associations/has_many_associations_test.rb
  50. +++ b/activerecord/test/cases/associations/has_many_associations_test.rb
  51. @@ -1017,4 +1017,20 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
  52. ActiveRecord::Base.store_full_sti_class = old
  53. end
  54.  
  55. + def test_empty_with_counter_sql
  56. + company = Firm.find(:first)
  57. + assert_sql /COUNT/i do
  58. + assert_equal false, company.clients.empty?
  59. + end
  60. + end
  61. +
  62. + def test_empty?
  63. + company = Firm.find(:first)
  64. + assert_queries(1) do
  65. + assert_equal false, company.plain_clients.empty?
  66. + end
  67. +
  68. + # should have used LIMIT, but because of different SQL adapters we can't check for that here.
  69. + assert_no_match /COUNT/i, $queries_executed.first
  70. + end
  71. end
  72. diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb
  73. index e21ffbb..fd0b09d 100644
  74. --- a/activerecord/test/cases/named_scope_test.rb
  75. +++ b/activerecord/test/cases/named_scope_test.rb
  76. @@ -231,4 +231,15 @@ class NamedScopeTest < ActiveRecord::TestCase
  77. assert topic.approved
  78. assert_equal 'lifo', topic.author_name
  79. end
  80. +
  81. + def test_empty_should_use_exists_when_not_loaded
  82. + ActiveRecord::NamedScope::Scope.any_instance.expects(:exists?).with({}).returns(true)
  83. + assert_equal false, Topic.approved.empty?
  84. + end
  85. +
  86. + def test_empty_should_not_use_exists_when_loaded
  87. + Topic.approved.to_a # load the association
  88. + Topic.approved.expects(:exists?).never
  89. + assert_equal false, Topic.approved.empty?
  90. + end
  91. end
  92. --
  93. 1.5.6.3
Add Comment
Please, Sign In to add comment