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

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -