Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8653

Show
Ignore:
Timestamp:
01/18/08 01:55:11 (4 months ago)
Author:
rick
Message:

Ensure that modifying has_and_belongs_to_many actions clear the query cache. Closes #10840 [john.andrews]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r8627 r8653  
    11*SVN* 
     2 
     3* Ensure that modifying has_and_belongs_to_many actions clear the query cache.  Closes #10840 [john.andrews] 
    24 
    35* Fix issue where Table#references doesn't pass a :null option to a *_type attribute for polymorphic associations.  Closes #10753 [railsjitsu] 
  • trunk/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb

    r8571 r8653  
    1515        create_record(attributes) { |record| insert_record(record) } 
    1616      end 
    17        
     17 
    1818      def create!(attributes = {}) 
    1919        create_record(attributes) { |record| insert_record(record, true) } 
     
    8181 
    8282          if @reflection.options[:insert_sql] 
    83             @owner.connection.execute(interpolate_sql(@reflection.options[:insert_sql], record)) 
     83            @owner.connection.insert(interpolate_sql(@reflection.options[:insert_sql], record)) 
    8484          else 
    8585            columns = @owner.connection.columns(@reflection.options[:join_table], "#{@reflection.options[:join_table]} Columns") 
     
    104104              "VALUES (#{attributes.values.join(', ')})" 
    105105 
    106             @owner.connection.execute(sql) 
     106            @owner.connection.insert(sql) 
    107107          end 
    108108 
     
    112112        def delete_records(records) 
    113113          if sql = @reflection.options[:delete_sql] 
    114             records.each { |record| @owner.connection.execute(interpolate_sql(sql, record)) } 
     114            records.each { |record| @owner.connection.delete(interpolate_sql(sql, record)) } 
    115115          else 
    116116            ids = quoted_record_ids(records) 
    117117            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})" 
    118             @owner.connection.execute(sql) 
     118            @owner.connection.delete(sql) 
    119119          end 
    120120        end 
  • trunk/activerecord/test/query_cache_test.rb

    r8570 r8653  
    44require 'fixtures/task' 
    55require 'fixtures/course' 
     6require 'fixtures/category' 
     7require 'fixtures/post' 
    68 
    79 
    810class QueryCacheTest < ActiveSupport::TestCase 
    9   fixtures :tasks, :topics 
     11  fixtures :tasks, :topics, :categories, :posts, :categories_posts 
    1012 
    1113  def test_find_queries 
     
    100102    end 
    101103  end 
     104 
     105  def test_cache_is_expired_by_habtm_update 
     106    ActiveRecord::Base.connection.expects(:clear_query_cache).times(2) 
     107    ActiveRecord::Base.cache do 
     108      c = Category.find(:first) 
     109      p = Post.find(:first) 
     110      p.categories << c 
     111    end 
     112  end 
     113 
     114  def test_cache_is_expired_by_habtm_delete 
     115    ActiveRecord::Base.connection.expects(:clear_query_cache).times(2) 
     116    ActiveRecord::Base.cache do 
     117      c = Category.find(:first) 
     118      p = Post.find(:first) 
     119      p.categories.delete_all 
     120    end 
     121  end 
    102122end 
    103123