提交 b155fdad 编写于 作者: A Andrew White

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

上级 84a4ef63
......@@ -183,7 +183,9 @@ def all(*args)
def exists?(id = nil)
id = id.id if ActiveRecord::Base === id
relation = select("1").limit(1)
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
......@@ -192,14 +194,13 @@ def exists?(id = nil)
relation = relation.where(table[primary_key].eq(id)) if id
end
relation.first ? true : false
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::JoinDependency.new(@klass, including, [])
join_dependency = construct_join_dependency_for_association_find
relation = construct_relation_for_association_find(join_dependency)
rows = connection.select_all(relation.to_sql, 'SQL', relation.bind_values)
join_dependency.instantiate(rows)
......@@ -207,6 +208,11 @@ def find_with_associations
[]
end
def construct_join_dependency_for_association_find
including = (@eager_load_values + @includes_values).uniq
ActiveRecord::Associations::JoinDependency.new(@klass, including, [])
end
def construct_relation_for_association_calculations
including = (@eager_load_values + @includes_values).uniq
join_dependency = ActiveRecord::Associations::JoinDependency.new(@klass, including, arel.froms.first)
......
......@@ -74,6 +74,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)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册