diff options
author | Eugene Gilburg <eugene.gilburg@gmail.com> | 2013-10-27 23:58:23 -0700 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-02-12 17:40:59 -0200 |
commit | d7a7a050e103def759d1e5694e5aaa35b1d03cd1 (patch) | |
tree | da6965c7c8404d1b884b95b13dc06dd3f741bfde /activerecord/lib/active_record/relation.rb | |
parent | 8d0fe8047a3c630d29224688ecfa4f4b26cb238a (diff) | |
download | rails-d7a7a050e103def759d1e5694e5aaa35b1d03cd1.tar.gz rails-d7a7a050e103def759d1e5694e5aaa35b1d03cd1.tar.bz2 rails-d7a7a050e103def759d1e5694e5aaa35b1d03cd1.zip |
Optimize none? and one? relation query methods to use LIMIT and COUNT.
Use SQL COUNT and LIMIT 1 queries for none? and one? methods if no block or limit is given,
instead of loading the entire collection to memory. The any? and many? methods already
follow this behavior.
[Eugene Gilburg & Rafael Mendonça França]
Diffstat (limited to 'activerecord/lib/active_record/relation.rb')
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 8073eb99dd..6bbec7c0c0 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -271,6 +271,15 @@ module ActiveRecord end end + # Returns true if there are no records. + def none? + if block_given? + to_a.none? { |*block_args| yield(*block_args) } + else + empty? + end + end + # Returns true if there are any records. def any? if block_given? @@ -280,6 +289,15 @@ module ActiveRecord end end + # Returns true if there is exactly one record. + def one? + if block_given? + to_a.one? { |*block_args| yield(*block_args) } + else + limit_value ? to_a.one? : size == 1 + end + end + # Returns true if there is more than one record. def many? if block_given? |