Skip to content

A platform for community discussion. Free, open, simple.

FIX: Improve performance for `Group.posts_for` (#6588)

David Taylorc3c9299c442a
diff --git a/app/models/group.rb b/app/models/group.rb
index 5c8d1376e3e..7093e55327d 100644
--- a/app/models/group.rb
+++ b/app/models/group.rb
@@ -193,10 +193,9 @@ class Group < ActiveRecord::Base
def posts_for(guardian, opts = nil)
opts ||= {}
- user_ids = group_users.map { |gu| gu.user_id }
- result = Post.includes(:user, :topic, topic: :category)
+ result = Post.includes(:topic, user: :groups, topic: :category)
.references(:posts, :topics, :category)
- .where(user_id: user_ids)
+ .where(groups: { id: id })
.where('topics.archetype <> ?', Archetype.private_message)
.where('topics.visible')
.where(post_type: Post.types[:regular])