Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Rails: Update Comments Count for All Topics in a Group
- ```ruby
- def update_topics_comments_count
- begin
- ActiveRecord::Base.connection.execute <<~SQL
- UPDATE topics
- SET comments_count = (SELECT count(1)
- FROM topic_comments
- WHERE topic_comments.topic_id = topics.id
- AND topics.group_id = '#{group.id}')
- SQL
- ensure
- ActiveRecord::Base.connection.close
- end
- end
- ```
Add Comment
Please, Sign In to add comment