-
-
Save blueplanet/4322792 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 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 |
This file contains hidden or 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
# coding: utf-8 | |
class Concern < ActiveRecord::Base | |
belongs_to :publication, counter_cache: true | |
end |
This file contains hidden or 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
- @publications.each do |pub| | |
= pub.concerns.size | |
= pub.publication_comments.size |
This file contains hidden or 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
# coding: utf-8 | |
class Publication < ActiveRecord::Base | |
has_many :concerns | |
has_many :publication_comments | |
end |
This file contains hidden or 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
# coding: utf-8 | |
class PublicationComment < ActiveRecord::Base | |
belongs_to :publication, counter_cache: true | |
end |
This file contains hidden or 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
# coding: utf-8 | |
class PublicationsController < ApplicationController | |
def index | |
@publications = Publication.all | |
end | |
end |
Author
blueplanet
commented
Dec 18, 2012
belongs_to :publication, counter_cache: true
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment