Feat: custom sort (#4864)

This commit is contained in:
Tejaswini Chile
2022-06-22 11:04:42 +05:30
committed by GitHub
parent a2204cf269
commit ffd102cdfe
7 changed files with 199 additions and 6 deletions

View File

@@ -23,6 +23,8 @@
# fk_rails_... (user_id => users.id) ON DELETE => cascade
#
class Mention < ApplicationRecord
include SortHandler
before_validation :ensure_account_id
validates :mentioned_at, presence: true
validates :account_id, presence: true
@@ -38,6 +40,17 @@ class Mention < ApplicationRecord
scope :latest, -> { order(mentioned_at: :desc) }
def self.last_user_message_at
# INNER query finds the last message created in the conversation group
# The outer query JOINS with the latest created message conversations
# Then select only latest incoming message from the conversations which doesn't have last message as outgoing
# Order by message created_at
Mention.joins(
"INNER JOIN (#{last_messaged_conversations.to_sql}) grouped_conversations
ON grouped_conversations.conversation_id = mentions.conversation_id"
).sort_on_last_user_message_at
end
private
def ensure_account_id