We Recommend

The Rails Way The Rails Way
Now, for the first time, there’s a comprehensive, authoritative guide to building production-quality software with Rails. Pioneering Rails developer Obie Fernandez and a team of experts illuminate the entire Rails API, along with the Ruby idioms, design approaches, libraries, and plug-ins that make Rails so valuable.


Posted By

bordalix on 07/13/07


Tagged

performance counter cache column


Versions (?)


Counter Cache Column


Published in: Rails 


A great way to improve the performance of your web app, by using a column in the projects table which stores the number of task associated with it, seen in railscast #23

  1. class AddTaskCount < ActiveRecord::Migration
  2. def seld.up
  3. add_column :projects, :tasks_count, :integer, :default => 0
  4. Project.reset_column_information
  5. Project.find(:all).each do |p|
  6. p.update_attribute :tasks_count, p.tasks.length
  7. end
  8. end
  9.  
  10. def self.down
  11. remove_column :projects, :tasks_count
  12. end
  13. end
  14.  
  15. --
  16.  
  17. class Task < ActiveRecord::Base
  18. belongs_to :project, :counter_cache => true
  19. end

Report this snippet 

You need to login to post a comment.