如何实现Mailboxer'搜索功能'在Rails中的Gem

如何实现Mailboxer'搜索功能'在Rails中的Gem

问题描述:

现在我正在使用称为'邮箱'的gem用于邮件系统。 https://github.com/ging/mailboxer 我想实现'关键字搜索功能',我应该能够搜索我的邮箱的会话和邮件,它应该来自收件箱,发件箱,垃圾或草稿。如何实现Mailboxer'搜索功能'在Rails中的Gem

但这种宝石给一个函数

search_messages(查询)

,但似乎不工作。

+0

你是什么意思,当你说它不工作?什么是错误?你是如何尝试使用它的?在这里,你确切的问题是什么? – vijoc

+0

嗨@ vijoc其实当我使用该功能,就像我得到错误,未定义的方法'搜索', 前 - current_user.search_messages('测试邮件'),在这里'测试邮件'我传递为该函数的查询。 – Gabbar

这里有一个function来检索主题,主体或收件人/发件人的用户名包含给定字符串的所有会话。你可以稍微调整一下,以适应你的情况,或者在他们的github回购上打开一个问题。

def conversations_for(query) 
    wildcarded_query = "%#{query}%" 
    user_ids = User.where('CONCAT(first_name, " ", last_name) LIKE :query', query: wildcarded_query).pluck(:id) 
    current_user.mailbox.conversations. 
         joins(:messages). 
         references('mailboxer_conversations, mailboxer_notifications, mailboxer_receipts, messages_mailboxer_conversations, mr, mn'). # To get rid of warnings 
         select('mailboxer_conversations.*, mailbox_type, trashed'). 
         where("mailboxer_notifications.subject LIKE :query 
           OR mailboxer_notifications.body LIKE :query 
           OR mailboxer_notifications.sender_id IN (:user_ids) 
           OR EXISTS (SELECT * FROM mailboxer_receipts mr 
                INNER JOIN mailboxer_notifications mn ON mn.id = mr.notification_id 
                WHERE mn.conversation_id = mailboxer_conversations.id 
                AND mr.notification_id IN (SELECT id FROM mailboxer_notifications 
                          WHERE sender_id = :current_user_id) 
                AND mr.receiver_id IN (:user_ids))", 
           query: wildcarded_query, user_ids: user_ids, current_user_id: current_user.id) 
end