From: Andy Allan Date: Wed, 30 Aug 2023 16:17:15 +0000 (+0100) Subject: Use Activerecord '#or' method for queries X-Git-Tag: live~1036^2 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/4af54ce93bd01a4094a4657cf9e54860a7e16bb2 Use Activerecord '#or' method for queries That let's us use relation names (like `sender`) and avoid dealing directly with ids. --- 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"