Created
May 27, 2013 09:04
-
-
Save andreaseriksson/5655982 to your computer and use it in GitHub Desktop.
RAILS Counter Cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Migration | |
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 | |
#Model | |
belongs_to :project, :counter_cache => true | |
#View | |
pluralize project.tasks.size, 'task' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment