Use chained scopes

This commit is contained in:
Jeffrey Hardy
2024-10-02 09:08:00 -04:00
parent 15fa3b7582
commit 49a3e569c2
2 changed files with 8 additions and 5 deletions
+5 -5
View File
@@ -4,13 +4,13 @@ class BubblesController < ApplicationController
before_action :set_bubble, only: %i[ show edit update ]
def index
@bubbles = @bucket.bubbles.reverse_chronologically
@most_active_bubbles = @bucket.bubbles.ordered_by_activity.limit(10)
if params[:tag_id]
@tag = Current.account.tags.find(params[:tag_id])
@bubbles = @tag.bubbles
@most_active_bubbles = @tag.bubbles.left_joins(:comments, :boosts).group(:id).order(Arel.sql("COUNT(comments.id) + COUNT(boosts.id) DESC")).limit(10)
else
@bubbles = @bucket.bubbles.order(created_at: :desc)
@most_active_bubbles = @bucket.bubbles.left_joins(:comments, :boosts).group(:id).order(Arel.sql("COUNT(comments.id) + COUNT(boosts.id) DESC")).limit(10)
@bubbles = @bubbles.joins(:tags).where(tags: @tag)
@most_active_bubbles = @most_active_bubbles.joins(:tags).where(tags: @tag)
end
end
+3
View File
@@ -15,5 +15,8 @@ class Bubble < ApplicationRecord
has_one_attached :image, dependent: :purge_later
scope :reverse_chronologically, -> { order(created_at: :desc, id: :desc) }
scope :ordered_by_activity, -> { left_joins(:comments, :boosts).group(:id).order(Arel.sql("COUNT(comments.id) + COUNT(boosts.id) DESC")) }
searchable_by :title, using: :bubbles_search_index
end