Counter Cache for a column with conditions?

后端 未结 4 1637
旧时难觅i
旧时难觅i 2020-11-30 06:34

I am new to the concept of counter caching and with some astronomical load times on one of my app\'s main pages, I believe I need to get going on it.

Most of the coun

4条回答
  •  执念已碎
    2020-11-30 07:17

    With regards to the conditions with counter_cache, I would read this blog post.

    The one thing you should do is add the following to the migration file:

     add_column :employees, :projects_count, :integer, :default => 0, :null => false
    
     Employee.reset_column_information
    
     Employee.all.each do |e|
       Employee.update_counters e.id, :projects_count => e.projects.length
     end
    

    So you current projects count can get migrated to the new projects_count that are associated with each Employee object. After that, you should be good to go.

提交回复
热议问题