`has_one` and `belongs_to` accessors don't add ORDER BY to the queries anymore.

Since Rails 4.0, we add an ORDER BY in the `first` method to ensure consistent
results among different database engines. But for singular associations this
behavior is not needed since we will have one record to return. As this
ORDER BY option can lead some performance issues we are removing it for singular
associations accessors.

Fixes #12623.
上级 e011258c
* `has_one` and `belongs_to` accessors don't add ORDER BY to the queries anymore.
Since Rails 4.0, we add an ORDER BY in the `first` method to ensure consistent results
among different database engines. But for singular associations this behavior is not needed
since we will have one record to return. As this ORDER BY option can lead some performance
issues we are removing it for singular associations accessors.
Fixes #12623.
*Rafael Mendonça França*
* Prepend table name for column names passed to `Relation#select`.
Example:
......
......@@ -39,7 +39,7 @@ def create_scope
end
def find_target
if record = scope.first
if record = scope.take
set_inverse_instance record
end
end
......
......@@ -28,6 +28,13 @@ def test_belongs_to
assert_equal companies(:first_firm).name, firm.name
end
def test_belongs_to_does_not_use_order_by
ActiveRecord::SQLCounter.clear_log
Client.find(3).firm
ensure
assert ActiveRecord::SQLCounter.log_all.all? { |sql| /order by/i !~ sql }, 'ORDER BY was used in the query'
end
def test_belongs_to_with_primary_key
client = Client.create(:name => "Primary key client", :firm_name => companies(:first_firm).name)
assert_equal companies(:first_firm).name, client.firm_with_primary_key.name
......
......@@ -22,6 +22,13 @@ def test_has_one
assert_equal Account.find(1).credit_limit, companies(:first_firm).account.credit_limit
end
def test_has_one_does_not_use_order_by
ActiveRecord::SQLCounter.clear_log
companies(:first_firm).account
ensure
assert ActiveRecord::SQLCounter.log_all.all? { |sql| /order by/i !~ sql }, 'ORDER BY was used in the query'
end
def test_has_one_cache_nils
firm = companies(:another_firm)
assert_queries(1) { assert_nil firm.account }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册