1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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
This commit is contained in:
Rick Olson 2008-01-02 21:39:49 +00:00
parent 744b1d7f4d
commit 0ad24df6ed
2 changed files with 10 additions and 2 deletions

View file

@ -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]

View file

@ -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.