aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2008-01-02 21:39:49 +0000
committerRick Olson <technoweenie@gmail.com>2008-01-02 21:39:49 +0000
commit0ad24df6ed389c1d57970c6e7650c5b1b7ae7676 (patch)
tree852a8f8fb56d0aacd3f67bf7fb2f0f247c014e6f /activerecord
parent744b1d7f4d2df583c77f99a33ae560ad64414c66 (diff)
downloadrails-0ad24df6ed389c1d57970c6e7650c5b1b7ae7676.tar.gz
rails-0ad24df6ed389c1d57970c6e7650c5b1b7ae7676.tar.bz2
rails-0ad24df6ed389c1d57970c6e7650c5b1b7ae7676.zip
Optimize ActiveRecord::Base#exists? to use #select_all instead of #find. Closes #10605 [jamesh, fcheung, protocool]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8531 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/base.rb10
2 files changed, 10 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 6306601cff..21ffbe08ae 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Optimize ActiveRecord::Base#exists? to use #select_all instead of #find. Closes #10605 [jamesh, fcheung, protocool]
+
* Don't unnecessarily load has_many associations in after_update callbacks. Closes #6822 [stopdropandrew, canadaduane]
* Eager belongs_to :include infers the foreign key from the association name rather than the class name. #10517 [Jonathan Viney]
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 5aba0f060a..2cdb43566c 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -548,8 +548,14 @@ module ActiveRecord #:nodoc:
# Person.exists?(:name => "David")
# Person.exists?(['name LIKE ?', "%#{query}%"])
def exists?(id_or_conditions)
- !find(:first, :select => "#{quoted_table_name}.#{primary_key}",
- :conditions => expand_id_conditions(id_or_conditions)).nil?
+ connection.select_all(
+ construct_finder_sql(
+ :select => "#{quoted_table_name}.#{primary_key}",
+ :conditions => expand_id_conditions(id_or_conditions),
+ :limit => 1
+ ),
+ "#{name} Exists"
+ ).size > 0
end
# Creates an object (or multiple objects) and saves it to the database, if validations pass.