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

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -