From 4af54ce93bd01a4094a4657cf9e54860a7e16bb2 Mon Sep 17 00:00:00 2001 From: Andy Allan Date: Wed, 30 Aug 2023 17:17:15 +0100 Subject: [PATCH] Use Activerecord '#or' method for queries That let's us use relation names (like `sender`) and avoid dealing directly with ids. --- app/controllers/messages_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index e0b5b05d3..adb53b43b 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -57,7 +57,7 @@ class MessagesController < ApplicationController # Destroy the message. def destroy - @message = Message.where("to_user_id = ? OR from_user_id = ?", current_user.id, current_user.id).find(params[:id]) + @message = Message.where(:recipient => current_user).or(Message.where(:sender => current_user.id)).find(params[:id]) @message.from_user_visible = false if @message.sender == current_user @message.to_user_visible = false if @message.recipient == current_user if @message.save && !request.xhr? @@ -109,7 +109,7 @@ class MessagesController < ApplicationController # Set the message as being read or unread. def mark - @message = Message.where("to_user_id = ? OR from_user_id = ?", current_user.id, current_user.id).find(params[:message_id]) + @message = Message.where(:recipient => current_user).or(Message.where(:sender => current_user)).find(params[:message_id]) if params[:mark] == "unread" message_read = false notice = t ".as_unread" -- 2.39.5