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

**URL:** https://meta.discourse.org/t/when-get-the-sum-of-some-objects-pluck-sum-can-be-changed-to-sum/68327
**Category:** Development
**Created:** [August 18, 2017, 5:05pm UTC](https://meta.discourse.org/t/when-get-the-sum-of-some-objects-pluck-sum-can-be-changed-to-sum/68327 "2017-08-18T17:05:09Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![junwen\_yang](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/junwen_yang/32/163137_2.png) [@junwen\_yang](https://meta.discourse.org/u/junwen_yang)
#### Post date: [August 18, 2017, 5:05pm UTC](https://meta.discourse.org/t/when-get-the-sum-of-some-objects-pluck-sum-can-be-changed-to-sum/68327/1 "2017-08-18T17:05:09Z")

</div>

app/models/trust\_level3\_requirements.rb:

```
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)
user.user_visits.sum(:posts_read)

```

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

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [August 18, 2017, 6:24pm UTC](https://meta.discourse.org/t/when-get-the-sum-of-some-objects-pluck-sum-can-be-changed-to-sum/68327/2 "2017-08-18T18:24:27Z")

</div>

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

---

<div class="post-metadata">

### Author: ![neil](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neil/32/102150_2.png) [@neil](https://meta.discourse.org/u/neil)
#### Post date: [August 18, 2017, 6:27pm UTC](https://meta.discourse.org/t/when-get-the-sum-of-some-objects-pluck-sum-can-be-changed-to-sum/68327/3 "2017-08-18T18:27:39Z")

</div>

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

---

<div class="post-metadata">

### Author: ![fantasticfears](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fantasticfears/32/119608_2.png) [@fantasticfears](https://meta.discourse.org/u/fantasticfears)
#### Post date: [August 23, 2017, 10:42am UTC](https://meta.discourse.org/t/when-get-the-sum-of-some-objects-pluck-sum-can-be-changed-to-sum/68327/5 "2017-08-23T10:42:56Z")

</div>

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?  
[https://meta.discoursecn.org/t/topic/2081](https://meta.discoursecn.org/t/topic/2081)

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [August 24, 2017, 11:56pm UTC](https://meta.discourse.org/t/when-get-the-sum-of-some-objects-pluck-sum-can-be-changed-to-sum/68327/6 "2017-08-24T23:56:39Z")

</div>

Thank you for the GitHub pull requests at [https://github.com/discourse/discourse](https://github.com/discourse/discourse) @junwen_yang! 🤗
