Assuming, There are tables called User, and Community
Community is created by a User, and it has user_id.
Community also has the number of Users who liked the community.
Let’s say
There are these communities. the number inside of () is the number of users who liked the community.
- community A (17), Community Owner is user_id ‘2’
- community B (35), Community Owner is user_id ‘1’
- community C (8), Community Owner is user_id ‘2’
I’d like to get summarized number of Users who liked User ‘2’ his community.
It should be 25 (17 + 8), here.
How can I get this?
It should be something like this? cached_votes_up has the number of users who like the community.
@user = User.find(2)
@communities = Community.find(@user).
@user.summarized_number = @communities.cached_votes_up.count
@user.save
You can use the ActiveRecord::Calculations#sum method: