ruby - Grouping by one column and summing multiple other columns in Rails -
i'm found ugly way need (currently in rails view, i'll move later), find articles of particular user, group them individual publications, , sum share counts of of articles user has added publication.
...but it's not pretty. if there's better way this, can advise?
=@user.articles.group(:publication).map |p| =p.publication =@user.articles.where("publication = ?", p.publication).sum(:twitter_count) =@user.articles.where("publication = ?", p.publication).sum(:facebook_count) =@user.articles.where("publication = ?", p.publication).sum(:linkedin_count)
this gives output (e.g.) nyt 12 18 14 bbc 45 46 47 cnn 75 54 78, pretty need.
however, @ present, it's outputting stuff on end - "[5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5] sum(:twitter_count)" - think .map i'm not sure why.
change =
-
in first line.
- @user.articles.group(:publication).map |p|
Comments
Post a Comment