When get the sum of some objects: pluck.sum can be changed to sum()

(Junwen Yang) #1


user.user_visits.where('visited_at > ?', time_period.days.ago).pluck(:posts_read).sum
app/models/trust_level3_requirements.rb:    user.user_visits.pluck(:posts_read).sum

could be changed to

user.user_visits.where('visited_at > ?', time_period.days.ago).sum(:posts_read)

since doing sum directly under db is faster than pluck and sum

(Sam Saffron) #2

Yeah, that sound right, @neil was there a reason for the “pull” and then “sum” stuff there?

(Neil Lalonde) #3

There probably wasn’t a good reason for pluck and sum. /shrug

(Erick Guan) #5

Hi, it looks like you are really interesting in development of Discourse. Mind also look for some advices here to tackle on the most intricate problems?

(Jeff Atwood) #6

Thank you for the GitHub pull requests at https://github.com/discourse/discourse @junwen_yang! :hugs: