mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-05-30 11:47:57 -04:00
24 lines
495 B
Ruby
24 lines
495 B
Ruby
# frozen_string_literal: true
|
|
|
|
class PostCommentCounterCache < ActiveRecord::Migration[5.2]
|
|
def change
|
|
change_table :posts do |t|
|
|
t.integer :comments_count, default: 0
|
|
end
|
|
reversible do |dir|
|
|
dir.up { set_counter_value }
|
|
end
|
|
end
|
|
|
|
def set_counter_value
|
|
execute <<~SQL.squish
|
|
UPDATE posts
|
|
SET comments_count = (
|
|
SELECT count(1)
|
|
FROM comments
|
|
WHERE comments.post_id = posts.id
|
|
)
|
|
SQL
|
|
end
|
|
end
|