From 49794485b6629a04d7efe324d7c91c0845dbdc2f Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Fri, 18 Jan 2008 01:55:11 +0000 Subject: Ensure that modifying has_and_belongs_to_many actions clear the query cache. Closes #10840 [john.andrews] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8653 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ .../has_and_belongs_to_many_association.rb | 10 +++++----- activerecord/test/query_cache_test.rb | 22 +++++++++++++++++++++- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index d3b5a38ec8..ed25689c51 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Ensure that modifying has_and_belongs_to_many actions clear the query cache. Closes #10840 [john.andrews] + * Fix issue where Table#references doesn't pass a :null option to a *_type attribute for polymorphic associations. Closes #10753 [railsjitsu] * Fixtures: removed support for the ancient pre-YAML file format. #10736 [John Barnette] diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb index eb7ff0581a..923fbd8522 100644 --- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb +++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb @@ -14,7 +14,7 @@ module ActiveRecord def create(attributes = {}) create_record(attributes) { |record| insert_record(record) } end - + def create!(attributes = {}) create_record(attributes) { |record| insert_record(record, true) } end @@ -80,7 +80,7 @@ module ActiveRecord end if @reflection.options[:insert_sql] - @owner.connection.execute(interpolate_sql(@reflection.options[:insert_sql], record)) + @owner.connection.insert(interpolate_sql(@reflection.options[:insert_sql], record)) else columns = @owner.connection.columns(@reflection.options[:join_table], "#{@reflection.options[:join_table]} Columns") @@ -103,7 +103,7 @@ module ActiveRecord "INSERT INTO #{@owner.connection.quote_table_name @reflection.options[:join_table]} (#{@owner.send(:quoted_column_names, attributes).join(', ')}) " + "VALUES (#{attributes.values.join(', ')})" - @owner.connection.execute(sql) + @owner.connection.insert(sql) end return true @@ -111,11 +111,11 @@ module ActiveRecord def delete_records(records) if sql = @reflection.options[:delete_sql] - records.each { |record| @owner.connection.execute(interpolate_sql(sql, record)) } + records.each { |record| @owner.connection.delete(interpolate_sql(sql, record)) } else ids = quoted_record_ids(records) sql = "DELETE FROM #{@owner.connection.quote_table_name @reflection.options[:join_table]} WHERE #{@reflection.primary_key_name} = #{@owner.quoted_id} AND #{@reflection.association_foreign_key} IN (#{ids})" - @owner.connection.execute(sql) + @owner.connection.delete(sql) end end diff --git a/activerecord/test/query_cache_test.rb b/activerecord/test/query_cache_test.rb index 12b52f6a35..242223161c 100644 --- a/activerecord/test/query_cache_test.rb +++ b/activerecord/test/query_cache_test.rb @@ -3,10 +3,12 @@ require 'fixtures/topic' require 'fixtures/reply' require 'fixtures/task' require 'fixtures/course' +require 'fixtures/category' +require 'fixtures/post' class QueryCacheTest < ActiveSupport::TestCase - fixtures :tasks, :topics + fixtures :tasks, :topics, :categories, :posts, :categories_posts def test_find_queries assert_queries(2) { Task.find(1); Task.find(1) } @@ -99,6 +101,24 @@ class QueryCacheExpiryTest < ActiveSupport::TestCase Task.create! end end + + def test_cache_is_expired_by_habtm_update + ActiveRecord::Base.connection.expects(:clear_query_cache).times(2) + ActiveRecord::Base.cache do + c = Category.find(:first) + p = Post.find(:first) + p.categories << c + end + end + + def test_cache_is_expired_by_habtm_delete + ActiveRecord::Base.connection.expects(:clear_query_cache).times(2) + ActiveRecord::Base.cache do + c = Category.find(:first) + p = Post.find(:first) + p.categories.delete_all + end + end end end -- cgit v1.2.3