RailsCasts Pro episodes are now free!
Learn more or hide this
The migration code shown in this screencast does not work in Rails 2.0. Instead please use the code below. Special thanks to Josh Owens for this blog post describing the problem and solution.
def self.up add_column :projects, :tasks_count, :integer, :default => 0 Project.reset_column_information Project.find(:all).each do |p| Project.update_counters p.id, :tasks_count => p.tasks.length end end def self.down remove_column :projects, :tasks_count end
belongs_to :project, :counter_cache => true
<%= pluralize project.tasks.size, 'task' %>
You can also find the original code here which should work for Rails versions before 2.0.