Skip to content
Browse files

2-0-stable: Ensure that modifying has_and_belongs_to_many actions cle…

…ar the query cache. References #10840 [john.andrews]  Merging [8653]

git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/2-0-stable@8828 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 569a78c commit 68477ad16dd0bebb14e421a7bbdefc0183f88947 @NZKoz NZKoz committed Feb 10, 2008
View
2 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]
* update_all ignores scoped :order and :limit, so post.comments.update_all doesn't try to include the comment order in the update statement. #10686 [Brendan Ribera]
View
12 activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
@@ -14,7 +14,7 @@ def build(attributes = {})
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 @@ def insert_record(record, force=true)
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,19 +103,19 @@ def insert_record(record, force=true)
"INSERT INTO #{@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
end
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 #{@reflection.options[:join_table]} WHERE #{@reflection.primary_key_name} = #{@owner.quoted_id} AND #{@reflection.association_foreign_key} IN (#{ids})"
- @owner.connection.execute(sql)
+ 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.delete(sql)
end
end
View
24 activerecord/test/query_cache_test.rb
@@ -3,10 +3,12 @@
require 'fixtures/reply'
require 'fixtures/task'
require 'fixtures/course'
+require 'fixtures/category'
+require 'fixtures/post'
-class QueryCacheTest < Test::Unit::TestCase
- fixtures :tasks, :topics
+class QueryCacheTest < ActiveSupport::TestCase
+ fixtures :tasks, :topics, :categories, :posts, :categories_posts
def test_find_queries
assert_queries(2) { Task.find(1); Task.find(1) }
@@ -99,6 +101,24 @@ def test_insert
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

0 comments on commit 68477ad

Please sign in to comment.
Something went wrong with that request. Please try again.