Clean up N+1 queries

This commit is contained in:
Joe Buhlig 2016-10-20 09:44:35 -05:00
parent 7c2905196f
commit 135eaf8a5e
3 changed files with 12 additions and 24 deletions

View File

@ -93,19 +93,13 @@ module DiscourseFeatureVoting
end end
def who_voted(topic) def who_voted(topic)
users = [] users = User.find(UserCustomField.where(name: "votes", value: topic.id).pluck(:user_id))
User.where(id: topic.who_voted).each do |user| ActiveModel::ArraySerializer.new(users, scope: Guardian.new(User.find_by(id: params["user_id"])), each_serializer: UserSerializer)
users.push(UserSerializer.new(user, scope: guardian, root: 'user'))
end
return users
end end
def who_super_voted(topic) def who_super_voted(topic)
users = [] users = User.find(UserCustomField.where(name: "super_votes", value: topic.id).pluck(:user_id))
User.where(id: topic.who_voted).each do |user| ActiveModel::ArraySerializer.new(users, scope: Guardian.new(User.find_by(id: params["user_id"])), each_serializer: UserSerializer)
users.push(UserSerializer.new(user, scope: guardian, root: 'user'))
end
return users
end end
end end
end end

View File

@ -86,8 +86,8 @@ export default createWidget('vote-count', {
}); });
function whoVotedAvatars(user) { function whoVotedAvatars(user) {
return { template: user.user.avatar_template, return { template: user.avatar_template,
username: user.user.username, username: user.username,
post_url: user.user.post_url, post_url: user.post_url,
url: Discourse.getURL('/users/') + user.user.username.toLowerCase() }; url: Discourse.getURL('/users/') + user.username.toLowerCase() };
} }

View File

@ -67,19 +67,13 @@ after_initialize do
end end
def who_voted def who_voted
users = [] users = User.find(UserCustomField.where(name: "votes", value: object.topic.id).pluck(:user_id))
User.where(id: object.topic.who_voted).each do |user| ActiveModel::ArraySerializer.new(users, scope: scope, each_serializer: UserSerializer)
users.push(UserSerializer.new(user, scope: scope, root: 'user'))
end
return users
end end
def who_super_voted def who_super_voted
users = [] users = User.find(UserCustomField.where(name: "votes", value: object.topic.id).pluck(:user_id))
User.where(id: object.topic.who_super_voted).each do |user| ActiveModel::ArraySerializer.new(users, scope: scope, each_serializer: UserSerializer)
users.push(UserSerializer.new(user, scope: scope, root: 'user'))
end
return users
end end
end end