Home>
I want to achieve
I want to output the total of integer values held by each User
There are the following DBs. (Item is integer)
<User id: 1, name: "guest1", item: 4>
<User id: 2, name: "guest2", item: 7>
<User id: 3, name: "guest3", item: 3>
..
..
How should I output the total of the item values of Uesr.all on the Controller side?
-
Answer # 1
Related articles
- ruby - the log is output even though the value obtained by the all method is used
- python - output of total card sorting value
- sql: how to get the item with the largest total value
- ruby - pass the value from the controller in the rails model
- nodejs - variable value is not referenced in output processing
- ruby - i want to change the way of arranging the hash value data obtained from the table
- ruby - i want to output csv with narrowed down conditions with rails
- ruby on rails - the value of find_by (id :) becomes nil
- ruby - i want to output the value assigned to a certain character in octal notation (preferably in other base notation)
- sort hashes in a ruby array by value
- [ruby] i don't know how to insert a new key and value in the specified part of hash
- html - value output with react textarea tag
- ruby - when saving the value after submitting
- ruby - i want to perform standard output with multiple lines and write/read multiple files
- ruby - display the value obtained via the intermediate table
- ruby - the value is not assigned successfully
- ruby - i can't get the value from the array i earned with news-api
- ruby - how to get the value of the intermediate table
- i want to get the value of the ruby embed code of the erb file with jquery
Related questions
- ruby - passing variables using render partial
- ruby - deployment error on heroku
- ruby - i want to use the search method for active hash data
- ruby - destroy action does not respond
- ruby - how do you see the rails controller relative path? (carrierwave directory deleted)
- ruby - when deploying on capistrano, i get the error "access denied for user'ec2-user' @'localhost' (using password: no)&qu
- javascript - run multiple times with js + each do
- how to reflect the layout downloaded from the external site in ruby on rails?
- ruby - how nice! function routes do not pass well (relation) [rails]
If you add them all
User.sum (: item)
When you want to narrow down by limit
User.limit (10) .sum (: item)
I want toSELECT SUM (
users
..item
) FROMusers
LIMIT 10And you will get the total of all.
So, do as follows.
User.limit (10) .pluck (: item) .sum
Or
SELECT SUM (item) FROM (SELECT
users
. * FROMusers
LIMIT 10) subquery