2013-01-11 27 views
0

我有兩個簡單的模型:ActiveRecord不使用counter_cache?

class Push < ActiveRecord::Base 
    has_many :opened_pushes 
end 

class OpenedPush < ActiveRecord::Base 
    belongs_to :push, :counter_cache => true 
end 

但是,如果我這樣做

a_push.opened_pushes.count 

它查詢數據庫,而不是使用opened_push_count目前在推塔。我認爲這已經夠聰明的了......這是怎麼回事?

+0

似乎從[源](https://github.com/rails/rails/blob/de1b687239832f336b9453493701955ab4d6d6a2/activerecord/lib/active_record/associations/builder/belongs_to.rb#L23),似乎' AR :: Base :: count'方法會受到影響。我猜你可能已經知道了,並且可能在其他東西上調用'.count'? –

回答

0

需要調用size,而不是lengthcount這將生成一個查詢。