From e4aec40578ea0dd2944f97f1ad60cfbdf4b3a44d Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Wed, 28 Nov 2018 03:34:20 +0900 Subject: More exercise singular association query Follow up ba4e68f577efc76f351d30a2914e29942b97830e. --- .../test/cases/associations/belongs_to_associations_test.rb | 6 +++--- activerecord/test/cases/associations/has_one_associations_test.rb | 6 +++--- .../test/cases/associations/has_one_through_associations_test.rb | 7 +++++++ activerecord/test/models/club.rb | 2 +- 4 files changed, 14 insertions(+), 7 deletions(-) (limited to 'activerecord') diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index c1a335bda8..d1e4ebe86b 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -33,10 +33,10 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase def test_belongs_to client = Client.find(3) + first_firm = companies(:first_firm) assert_sql(/LIMIT|ROWNUM <=|FETCH FIRST/) do - firm = client.firm - assert_not_nil firm - assert_equal companies(:first_firm).name, firm.name + assert_equal first_firm, client.firm + assert_equal first_firm.name, client.firm.name end end diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index a032f130ca..bf574f6637 100644 --- a/activerecord/test/cases/associations/has_one_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_associations_test.rb @@ -23,10 +23,10 @@ class HasOneAssociationsTest < ActiveRecord::TestCase def test_has_one firm = companies(:first_firm) + first_account = Account.find(1) assert_sql(/LIMIT|ROWNUM <=|FETCH FIRST/) do - account = firm.account - assert_equal account, Account.find(1) - assert_equal Account.find(1).credit_limit, account.credit_limit + assert_equal first_account, firm.account + assert_equal first_account.credit_limit, firm.account.credit_limit end end diff --git a/activerecord/test/cases/associations/has_one_through_associations_test.rb b/activerecord/test/cases/associations/has_one_through_associations_test.rb index 0309663943..69b4872519 100644 --- a/activerecord/test/cases/associations/has_one_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_through_associations_test.rb @@ -35,6 +35,13 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase assert_equal clubs(:boring_club), @member.club end + def test_has_one_through_executes_limited_query + boring_club = clubs(:boring_club) + assert_sql(/LIMIT|ROWNUM <=|FETCH FIRST/) do + assert_equal boring_club, @member.general_club + end + end + def test_creating_association_creates_through_record new_member = Member.create(name: "Chris") new_member.club = Club.create(name: "LRUG") diff --git a/activerecord/test/models/club.rb b/activerecord/test/models/club.rb index 2006e05fcf..13e72e9c50 100644 --- a/activerecord/test/models/club.rb +++ b/activerecord/test/models/club.rb @@ -10,7 +10,7 @@ class Club < ActiveRecord::Base has_many :favourites, -> { where(memberships: { favourite: true }) }, through: :memberships, source: :member - scope :general, -> { left_joins(:category).where(categories: { name: "General" }) } + scope :general, -> { left_joins(:category).where(categories: { name: "General" }).unscope(:limit) } private -- cgit v1.2.3