Files
once-campfire/app/models/membership.rb
T
Cursor Agent 88fbeedc75 Avoid a double join alias when loading shared sidebar rooms
Chaining without_direct_rooms with with_ordered_room joined rooms as
`room` while still ordering on `rooms.name`. Load shared rooms in one
scope instead.

Co-authored-by: Thomas Klemm <github@tklemm.eu>
2026-10-05 12:36:42 +00:00

27 lines
938 B
Ruby

class Membership < ApplicationRecord
include Connectable
belongs_to :room
belongs_to :user
after_destroy_commit { user.reset_remote_connections }
enum :involvement, %w[ invisible nothing mentions everything ].index_by(&:itself), prefix: :involved_in
scope :with_ordered_room, -> { includes(:room).joins(:room).order("LOWER(rooms.name)") }
scope :with_direct_rooms, -> { includes(:room).joins(:room).where(rooms: { type: "Rooms::Direct" }).order(rooms: { updated_at: :desc }) }
scope :with_ordered_shared_rooms, -> { includes(:room).joins(:room).where.not(rooms: { type: "Rooms::Direct" }).order("LOWER(rooms.name)") }
scope :without_direct_rooms, -> { joins(:room).where.not(room: { type: "Rooms::Direct" }) }
scope :visible, -> { where.not(involvement: :invisible) }
scope :unread, -> { where.not(unread_at: nil) }
def read
update!(unread_at: nil)
end
def unread?
unread_at.present?
end
end