L
L
lavezzi12015-08-19 23:05:17
Ruby on Rails
lavezzi1, 2015-08-19 23:05:17

How to display sum of column values ​​from a table?

Hello.
Let's say we have the following scheme:
users (has_many posts)
posts(belongs_to users)
---pages (let's say)
We need to display in the statistics how many pages a particular user has in all his posts. How to implement?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Demidenko, 2015-08-19
@lavezzi1

User.find(params[:user_id]).posts.sum(:pages)

Павел Кононенко, 2015-08-19
@premas

pages это что? Если количество страниц для поста, то:
Post.where(user_id: params[:user_id]).sum(:pages)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question