How to display highest rated albums through a has_many reviews relationship

后端 未结 1 1345
南笙
南笙 2021-01-29 00:00

I\'m setting up a simple ruby/rails app where users can review albums. On an album\'s show page I average all the user reviews associated with that album through this code in my

相关标签:
1条回答
  • 2021-01-29 00:25

    try @albums = Album.joins(:reviews).select("album.id, avg(reviews.rating) as average_rating).group("album.id").order("average_rating DESC")

    I extrapolated it from: Ruby on Rails: Order users based on average ratings with most reviews?

    Update:

    try this instead: @albums = Album.joins(:reviews).select("*, avg(reviews.rating) as average_rating").group("albums.id").order("average_rating DESC")

    0 讨论(0)
提交回复
热议问题