2010-12-15 68 views
0

的ActiveRecord的禁用實現QueryCache我想禁用ActiveRecord的紅寶石的查詢緩存,但它不工作,代碼如下:如何從紅寶石

def test_transaction 
    ActiveRecord::Base.connection.execute("create table if not exists t(name varchar(100))") 
    ActiveRecord::Base.connection.uncached do 
    begin 
    ActiveRecord::Base.transaction do 
     ActiveRecord::Base.connection.execute("insert into t values('one')") 
     ActiveRecord::Base.connection.execute("insert into t values('two')") 
     ActiveRecord::Base.connection.execute("insert into t values('three')") 
     ActiveRecord::Base.connection.execute("insert into t values('four')") 
     result = ActiveRecord::Base.connection.select_all("select count(*) as total from t")[0]['total'].to_i 
     assert_equal(0, result) 
    end 
    ensure 
    ActiveRecord::Base.connection.execute("drop table if exists t") 
    end 
end 
end 

其實無論何時代碼運行在選擇之前,我通過控制檯登錄mysql並執行相同的select查詢,結果總數爲零,所以沒關係,但在代碼執行select後,結果爲4,也就是說select必須從查詢緩存,但這是我不打算,所以任何人都可以爲我提供任何幫助?

預先感謝您!

回答

1

之後直接在mysql控制檯上做實驗,最後發現它與ActiveRecord沒有關係,這是由於mysql事務的機制。