diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-01-21 14:33:16 -0200 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-01-21 14:41:52 -0200 |
commit | b0a8ef140e4cc95fa5d3888699fd3d72fd720172 (patch) | |
tree | 36e75ffa27dcd74dc0d08382098a552df53fab6d /activerecord | |
parent | e011258c30b61f30e40fb2d9b2f58eb1f700dfd5 (diff) | |
download | rails-b0a8ef140e4cc95fa5d3888699fd3d72fd720172.tar.gz rails-b0a8ef140e4cc95fa5d3888699fd3d72fd720172.tar.bz2 rails-b0a8ef140e4cc95fa5d3888699fd3d72fd720172.zip |
`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.
Diffstat (limited to 'activerecord')
4 files changed, 26 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 9b2e9c4660..a108a1b2a9 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,14 @@ +* `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: diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb index e4500af5b2..399aff378a 100644 --- a/activerecord/lib/active_record/associations/singular_association.rb +++ b/activerecord/lib/active_record/associations/singular_association.rb @@ -39,7 +39,7 @@ module ActiveRecord end def find_target - if record = scope.first + if record = scope.take set_inverse_instance record end end diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index 3205d0c28b..2283ba66db 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -28,6 +28,13 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase 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 diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index 5a41461edf..d4edef03d6 100644 --- a/activerecord/test/cases/associations/has_one_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_associations_test.rb @@ -22,6 +22,13 @@ class HasOneAssociationsTest < ActiveRecord::TestCase 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 } |