Skip to content
Browse files

Change exists? so that it doesn't instantiate records [#6127 state:re…

…solved]
  • Loading branch information...
1 parent 235a414 commit 827e5de60f27c6b559640eda7b79857050a72d9e @pixeltrix pixeltrix committed Mar 29, 2011
View
20 activerecord/lib/active_record/relation/finder_methods.rb
@@ -171,27 +171,35 @@ def all(*args)
def exists?(id = nil)
id = id.id if ActiveRecord::Base === id
+ join_dependency = construct_join_dependency_for_association_find
+ relation = construct_relation_for_association_find(join_dependency)
+ relation = relation.except(:select).select("1").limit(1)
+
case id
when Array, Hash
- where(id).exists?
+ relation = relation.where(id)
else
- relation = select(primary_key).limit(1)
- relation = relation.where(primary_key.eq(id)) if id
- relation.first ? true : false
+ relation = relation.where(table[primary_key.name].eq(id)) if id
end
+
+ connection.select_value(relation.to_sql) ? true : false
end
protected
def find_with_associations
- including = (@eager_load_values + @includes_values).uniq
- join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, including, nil)
+ join_dependency = construct_join_dependency_for_association_find
rows = construct_relation_for_association_find(join_dependency).to_a
join_dependency.instantiate(rows)
rescue ThrowResult
[]
end
+ def construct_join_dependency_for_association_find
+ including = (@eager_load_values + @includes_values).uniq
+ join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, including, nil)
+ end
+
def construct_relation_for_association_calculations
including = (@eager_load_values + @includes_values).uniq
join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, including, arel.join_sql)
View
5 activerecord/test/cases/finder_test.rb
@@ -126,6 +126,11 @@ def test_exists_with_scoped_include
end
end
+ def test_exists_does_not_instantiate_records
+ Developer.expects(:instantiate).never
+ Developer.exists?
+ end
+
def test_find_by_array_of_one_id
assert_kind_of(Array, Topic.find([ 1 ]))
assert_equal(1, Topic.find([ 1 ]).length)

0 comments on commit 827e5de

Please sign in to comment.
Something went wrong with that request. Please try again.