sql - Query for Joining Multiple Associations -


i managed make query joined table. wonder how add join table (e.g. publication_comments), count how many comments publication have, sum publication_comments_count reviews_count , order in desc order.

i have read rails guide , many example on stackoverflow, still need assist on syntax , how should gather pieces in 1 place. thanks.

@publication = publication.all(                           joins: :reviews,                           select: '"publications".*, count("reviews".id) reviews_count',                            group: '"publications".id',                           order: "reviews_count desc") 

activerecord allows supply string joins method. can write raw sql join conditions:

publication.select("..., count(publication_comments.id) pub_count")  .join('join reviews on (..) join publication_comments')  .group('publications.id' 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -