diff options
author | Peter Marklund <peter_marklund@fastmail.fm> | 2009-05-14 09:30:16 +0200 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2009-05-14 20:42:00 +1200 |
commit | afcbdfc15f919a470e4cfca97fb0084eebd2ab1f (patch) | |
tree | 2a05aa38fb247d8c1abc92a3c5926d9a3fdacc79 | |
parent | 49a84ff69ca4fc4db821ca3b5a5926d07832c845 (diff) | |
download | rails-afcbdfc15f919a470e4cfca97fb0084eebd2ab1f.tar.gz rails-afcbdfc15f919a470e4cfca97fb0084eebd2ab1f.tar.bz2 rails-afcbdfc15f919a470e4cfca97fb0084eebd2ab1f.zip |
Changed ActiveRecord::Base#exists? to invoke find_initial so that it is compatible with, and doesn't lose, :include scopes (references to eager loaded tables)
Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#2543 state:committed]
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 11 | ||||
-rw-r--r-- | activerecord/test/cases/finder_test.rb | 6 |
2 files changed, 9 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index b9ba727a3f..54ec0e841f 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -697,14 +697,9 @@ module ActiveRecord #:nodoc: # Person.exists?(['name LIKE ?', "%#{query}%"]) # Person.exists? def exists?(id_or_conditions = {}) - 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 + find_initial( + :select => "#{quoted_table_name}.#{primary_key}", + :conditions => expand_id_conditions(id_or_conditions)) ? true : false end # Creates an object (or multiple objects) and saves it to the database, if validations pass. diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index ad4588db69..d0d7094e30 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -119,6 +119,12 @@ class FinderTest < ActiveRecord::TestCase Address.new(existing_address.street + "1", existing_address.city, existing_address.country)) end + def test_exists_with_scoped_include + Developer.with_scope(:find => { :include => :projects, :order => "projects.name" }) do + assert Developer.exists? + end + end + def test_find_by_array_of_one_id assert_kind_of(Array, Topic.find([ 1 ])) assert_equal(1, Topic.find([ 1 ]).length) |