Rails 4 Sum by Model Method

后端 未结 4 1214
夕颜
夕颜 2021-02-13 02:07

In my app, I have a User model, with a goal_ytd method, which performs some calculations.

In a controller, I have a variable @users

4条回答
  •  日久生厌
    2021-02-13 02:48

    On Rails 4.1

    If goal_ydt is a column in the users table:

    @users.sum(:goal_ydt)
    

    If goal_ydt is a method in User class:

    @users.to_a.sum(&:goal_ydt)
    

提交回复
热议问题