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
Post a Comment