Rails counter_cache not updating correctly

前端 未结 5 732
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-04 00:00

Using Rails 3.1.3 and I\'m trying to figure out why our counter caches aren\'t being updated correctly when changing the parent record id via update_attributes.

         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-04 00:37

    The counter_cache function is designed to work through the association name, not the underlying id column. In your test, instead of:

    registration.update_attributes(:event_id => other_event.id)
    

    try

    registration.update_attributes(:event => other_event)
    

    More information can be found here: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

提交回复
热议问题