diff options
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 5 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/collection_association.rb | 5 | ||||
-rw-r--r-- | activerecord/lib/active_record/query_cache.rb | 12 | ||||
-rw-r--r-- | activerecord/test/cases/associations/has_many_associations_test.rb | 25 | ||||
-rw-r--r-- | activerecord/test/cases/query_cache_test.rb | 16 | ||||
-rw-r--r-- | activerecord/test/schema/schema.rb | 2 |
6 files changed, 28 insertions, 37 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index f780029a18..bbf23c90df 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,8 @@ +* Query cache was unavailable when entering the ActiveRecord::Base.cache block + without being connected. + + *Tsukasa Oishi* + * Previously, when building records using a `has_many :through` association, if the child records were deleted before the parent was saved, they would still be persisted. Now, if child records are deleted before the parent is saved diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index c5ed6d41dd..bbf3dbb75e 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -44,10 +44,7 @@ module ActiveRecord if loaded? target.pluck(reflection.association_primary_key) else - @association_ids ||= ( - column = "#{reflection.quoted_table_name}.#{reflection.association_primary_key}" - scope.pluck(column) - ) + @association_ids ||= scope.pluck(reflection.association_primary_key) end end diff --git a/activerecord/lib/active_record/query_cache.rb b/activerecord/lib/active_record/query_cache.rb index ec246e97bc..e4c2e1f86f 100644 --- a/activerecord/lib/active_record/query_cache.rb +++ b/activerecord/lib/active_record/query_cache.rb @@ -5,20 +5,20 @@ module ActiveRecord # Enable the query cache within the block if Active Record is configured. # If it's not, it will execute the given block. def cache(&block) - if connected? - connection.cache(&block) - else + if configurations.empty? yield + else + connection.cache(&block) end end # Disable the query cache within the block if Active Record is configured. # If it's not, it will execute the given block. def uncached(&block) - if connected? - connection.uncached(&block) - else + if configurations.empty? yield + else + connection.uncached(&block) end end end diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 169e1a5449..a936017ae3 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -745,35 +745,35 @@ class HasManyAssociationsTest < ActiveRecord::TestCase firm = Firm.all.merge!(order: "id").first collection = firm.clients - original_object_id = collection.first.object_id - assert_equal original_object_id, collection.first.object_id, "Expected second call to #first to cache the same object" + original_object = collection.first + assert_same original_object, collection.first, "Expected second call to #first to cache the same object" # It should return a different object, since the association has been reloaded - assert_not_equal original_object_id, firm.clients.first.object_id, "Expected #first to return a new object" + assert_not_same original_object, firm.clients.first, "Expected #first to return a new object" end def test_find_first_after_reset firm = Firm.all.merge!(order: "id").first collection = firm.clients - original_object_id = collection.first.object_id - assert_equal original_object_id, collection.first.object_id, "Expected second call to #first to cache the same object" + original_object = collection.first + assert_same original_object, collection.first, "Expected second call to #first to cache the same object" collection.reset # It should return a different object, since the association has been reloaded - assert_not_equal original_object_id, collection.first.object_id, "Expected #first after #reload to return a new object" + assert_not_same original_object, collection.first, "Expected #first after #reset to return a new object" end def test_find_first_after_reload firm = Firm.all.merge!(order: "id").first collection = firm.clients - original_object_id = collection.first.object_id - assert_equal original_object_id, collection.first.object_id, "Expected second call to #first to cache the same object" - collection.reset + original_object = collection.first + assert_same original_object, collection.first, "Expected second call to #first to cache the same object" + collection.reload # It should return a different object, since the association has been reloaded - assert_not_equal original_object_id, collection.first.object_id, "Expected #first after #reload to return a new object" + assert_not_same original_object, collection.first, "Expected #first after #reload to return a new object" end def test_find_all_with_include_and_conditions @@ -2354,8 +2354,9 @@ class HasManyAssociationsTest < ActiveRecord::TestCase car = Car.create! bulb = Bulb.create! name: "other", car: car - assert_equal bulb, Car.find(car.id).all_bulbs.first - assert_equal bulb, Car.includes(:all_bulbs).find(car.id).all_bulbs.first + assert_equal [bulb], Car.find(car.id).all_bulbs + assert_equal [bulb], Car.includes(:all_bulbs).find(car.id).all_bulbs + assert_equal [bulb], Car.eager_load(:all_bulbs).find(car.id).all_bulbs end test "raises RecordNotDestroyed when replaced child can't be destroyed" do diff --git a/activerecord/test/cases/query_cache_test.rb b/activerecord/test/cases/query_cache_test.rb index c2d40f2940..3962453f75 100644 --- a/activerecord/test/cases/query_cache_test.rb +++ b/activerecord/test/cases/query_cache_test.rb @@ -321,18 +321,7 @@ class QueryCacheTest < ActiveRecord::TestCase end end - def test_cache_is_available_when_connection_is_connected - conf = ActiveRecord::Base.configurations - - ActiveRecord::Base.configurations = {} - Task.cache do - assert_queries(1) { Task.find(1); Task.find(1) } - end - ensure - ActiveRecord::Base.configurations = conf - end - - def test_cache_is_not_available_when_using_a_not_connected_connection + def test_cache_is_available_when_using_a_not_connected_connection with_temporary_connection_pool do spec_name = Task.connection_specification_name conf = ActiveRecord::Base.configurations["arunit"].merge("name" => "test2") @@ -349,8 +338,7 @@ class QueryCacheTest < ActiveRecord::TestCase end ActiveRecord::FixtureSet.create_fixtures(self.class.fixture_path, ["tasks"], {}, ActiveRecord::Base) end - Task.connection # warmup postgresql connection setup queries - assert_queries(2) { Task.find(1); Task.find(1) } + assert_queries(1) { Task.find(1); Task.find(1) } ensure ActiveRecord::Base.connection_handler.remove_connection(Task.connection_specification_name) Task.connection_specification_name = spec_name diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index d7ceb6d4ef..f534e9c00e 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -107,7 +107,7 @@ ActiveRecord::Schema.define do t.boolean :has_fun, null: false, default: false end - create_table :bulbs, force: true do |t| + create_table :bulbs, primary_key: "ID", force: true do |t| t.integer :car_id t.string :name t.boolean :frickinawesome, default: false |