Guest User

Untitled

a guest
Jul 17th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. # Rails: Update Comments Count for All Topics in a Group
  2.  
  3. ```ruby
  4. def update_topics_comments_count
  5. begin
  6. ActiveRecord::Base.connection.execute <<~SQL
  7. UPDATE topics
  8. SET comments_count = (SELECT count(1)
  9. FROM topic_comments
  10. WHERE topic_comments.topic_id = topics.id
  11. AND topics.group_id = '#{group.id}')
  12. SQL
  13. ensure
  14. ActiveRecord::Base.connection.close
  15. end
  16. end
  17. ```
Add Comment
Please, Sign In to add comment