Skip to content

Instantly share code, notes, and snippets.

@blueplanet
Forked from CodeIQ/concern.rb
Created December 17, 2012 22:10
Show Gist options
  • Save blueplanet/4322792 to your computer and use it in GitHub Desktop.
Save blueplanet/4322792 to your computer and use it in GitHub Desktop.
# coding: utf-8
class AddCountCacheToPublications < ActiveRecord::Migration
def change
add_column :publications, :concerns_count, :integer, null: false, default: 0
add_column :publications, :publication_comments_count, :integer, null: false, default: 0
Publication.reset_column_information
Publication.all.each do |p|
Publication.update_counters p.id, concerns_count: p.concerns.count, publication_comments_count: p.publication_comments.count
end
end
end
# coding: utf-8
class Concern < ActiveRecord::Base
belongs_to :publication, counter_cache: true
end
- @publications.each do |pub|
= pub.concerns.size
= pub.publication_comments.size
# coding: utf-8
class Publication < ActiveRecord::Base
has_many :concerns
has_many :publication_comments
end
# coding: utf-8
class PublicationComment < ActiveRecord::Base
belongs_to :publication, counter_cache: true
end
# coding: utf-8
class PublicationsController < ApplicationController
def index
@publications = Publication.all
end
end
@blueplanet
Copy link
Author

belongs_to :publication, counter_cache: true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment