diff --git a/app/controllers/bubbles_controller.rb b/app/controllers/bubbles_controller.rb index f6e73ba92..0e74721fe 100644 --- a/app/controllers/bubbles_controller.rb +++ b/app/controllers/bubbles_controller.rb @@ -9,8 +9,8 @@ class BubblesController < ApplicationController if params[:tag_id] @tag = Current.account.tags.find(params[:tag_id]) - @bubbles = @bubbles.joins(:tags).where(tags: @tag) - @most_active_bubbles = @most_active_bubbles.joins(:tags).where(tags: @tag) + @bubbles = @bubbles.tagged_with(@tag) + @most_active_bubbles = @most_active_bubbles.tagged_with(@tag) end end diff --git a/app/models/bubble.rb b/app/models/bubble.rb index 9a1a66a2c..2a2d2c214 100644 --- a/app/models/bubble.rb +++ b/app/models/bubble.rb @@ -1,5 +1,5 @@ class Bubble < ApplicationRecord - include Assignable, Colored, Searchable + include Assignable, Colored, Searchable, Taggable belongs_to :bucket belongs_to :creator, class_name: "User", default: -> { Current.user } @@ -7,9 +7,6 @@ class Bubble < ApplicationRecord has_many :comments, dependent: :destroy has_many :boosts, dependent: :destroy - has_many :taggings, dependent: :destroy - has_many :tags, through: :taggings - has_one_attached :image, dependent: :purge_later scope :reverse_chronologically, -> { order(created_at: :desc, id: :desc) } diff --git a/app/models/bubble/taggable.rb b/app/models/bubble/taggable.rb new file mode 100644 index 000000000..33c53d160 --- /dev/null +++ b/app/models/bubble/taggable.rb @@ -0,0 +1,10 @@ +module Bubble::Taggable + extend ActiveSupport::Concern + + included do + has_many :taggings, dependent: :destroy + has_many :tags, through: :taggings + + scope :tagged_with, ->(tag) { joins(:taggings).where(taggings: { tag: tag }) } + end +end