There's a view where I want to display the contents of
for example:
recipe.user.name
recipe.name
recipe.picture
recipe.created_at
sorted by
recipe.likes.count
Controller:
@recipes_by_likes = Recipe.likes_count.paginate(page: params[:page])
Model:
Recipe has_many :likes
and
Like belongs_to :recipe
Scope:
scope :likes_count, -> {
select('recipes.*, COUNT(likes.id) AS likes_count').
joins(:likes).
order('likes_count DESC')
}
but that gives me
SELECT recipes.*, COUNT(likes.id) AS likes_count
FROM "recipes"
INNER JOIN "likes" ON "likes"."recipe_id" = "recipes"."id"
ORDER BY "recipes"."created_at" DESC, likes_count DESC
LIMIT 30 OFFSET 0
which returns only 1 (wrong) result.