Skip to content

Instantly share code, notes, and snippets.

@mikekellyio
Last active December 28, 2015 22:19
Show Gist options
  • Save mikekellyio/7570582 to your computer and use it in GitHub Desktop.
Save mikekellyio/7570582 to your computer and use it in GitHub Desktop.
def AddCounterCache < ActiveRecord::Migration
def change
add_column :users, :posts_count, :integer
end
end
def Post < ActiveRecord::Base
belongs_to :user, counter_cache: true
end
<ul>
<% @users.each do |user| %>
<li>
<%= @user.name %> (<%= @user.posts.size %>)
</li>
<% end %>
</ul>
def User < ActiveRecord::Base
has_many :posts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment