I've added forum_threads_count
and forum_posts_count
columns to the Forums table. The forum_threads_count
works just fine. The forum_posts_count
has been reset to "0" instead of showing all of the forum posts that have been created before I added the counter cache columns. The relationships are: Forum has_many :forum_threads
, ForumThreads has_many :forum_posts, and Forum has_many :forum_posts, through: :forum_threads
.
I later found out that I can't use counter_cache with a has_many through:
relationship. So I wrote some private methods to add after_create
/after_destroy
calls to to increment/decrement the counter. The counter works, it's just that it's still not accounting for all of the forum posts that were created before adding these columns to the Forum table. I feel like it's something wrong with how I wrote the migration. Please help and thank you in advance. I appreciate everyone on this site helping people out.
"...add_counters_to_forums_table.rb"(migration file)
class AddCountersToForumsTableAgain < ActiveRecord::Migration
def self.up
change_table :forums do |t|
t.integer :forum_threads_count, :forum_posts_count, default: 0
end
Forum.reset_column_information
Forum.all.pluck(:id).each do |id|
Forum.reset_counters(id, :forum_posts)
Forum.reset_counters(id, :forum_threads)
end
end
def self.down
change_table :forums do |t|
t.remove :forum_threads_count, :forum_posts_count
end
end
end
models/forum.rb
class Forum < ActiveRecord::Base
has_many :forum_threads, -> { order ('updated_at DESC') }, dependent: :destroy
accepts_nested_attributes_for :forum_threads
has_many :forum_posts, through: :forum_threads
accepts_nested_attributes_for :forum_posts
end
models/forum_thread.rb
class ForumThread < ActiveRecord::Base
belongs_to :user
belongs_to :forum, counter_cache: true
has_many :forum_posts, dependent: :destroy
accepts_nested_attributes_for :forum_posts
end
models/forum_post.rb
class ForumPost < ActiveRecord::Base
belongs_to :forum_thread, touch: true
belongs_to :forum
belongs_to :user
after_create :increment_forum_posts_count
after_destroy :decrement_forum_posts_count
private
def increment_forum_posts_count
Forum.increment_counter( 'forum_posts_count', self.forum_thread.forum.id )
end
def decrement_forum_posts_count
Forum.decrement_counter( 'forum_posts_count', self.forum_thread.forum.id )
end
end
views/forums/index.html.erb
<%= render 'shared/page_title', title: "Forums" %>
<div class="col-md-10 col-md-offset-1">
<div class="actions">
<%= link_to "Create New Forum", new_forum_path, class: 'btn btn-primary' %>
<div class="pull-right">
<%= form_tag @forum_thread, method: :get do |f| %>
<%= text_field_tag :q, nil, class: 'form-control', placeholder: 'Search...' %>
<% end %>
</div>
</div>
# LIST FORUMS WITH THREADS AND POSTS COUNTER CACHE
<div class="list-group">
<% @forums.each do |forum| %>
<a href="<%= forum_forum_threads_path(forum.id, @forum_threads) %>" class="list-group-item">
<h3><%= forum.title %>
<div class="pull-right small">
<%= pluralize forum.forum_threads.size, 'thread' %> |
<%= pluralize forum.forum_posts.size, 'post' %>
</div>
</h3>
</a>
<% end %>
</div>