LoginSignup
5
1

More than 5 years have passed since last update.

ActiveRecordのsumメソッド

Posted at

微妙に想定と違ったのでメモ
https://apidock.com/rails/ActiveRecord/Calculations/sum

Post.limit(3).sum(:review_count)

これが

SELECT SUM(`posts`.`review_count`) FROM `posts` LIMIT 3

こうなる(SUM (カラムの総和) の結果 (1件) に対してLIMITが走る)

rubyのsumメソッドで無理やり対処

Post.limit(3).pluck(:view_count).sum

5
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1