Ok I have a messages table:
+-----+-----------+--------------+--------------+-----------+--------+--------------+--------------+
| id | sender_id | recipient_id | body | parent_id | status | created_at | updated_at |
+-----+-----------+--------------+--------------+-----------+--------+--------------+--------------+
| 220 | 4 | 1 | hi | 220 | 0 | 2012-02-1... | 2012-02-1... |
| 221 | 1 | 4 | hey | 220 | 0 | 2012-02-1... | 2012-02-1... |
| 222 | 4 | 1 | hi | 220 | 0 | 2012-02-1... | 2012-02-1... |
| 223 | 1 | 4 | hi | 220 | 0 | 2012-02-1... | 2012-02-1... |
| 232 | 1 | 4 | good | 220 | 0 | 2012-02-1... | 2012-02-1... |
+-----+-----------+--------------+--------------+-----------+--------+--------------+--------------+
I’m trying to work out a way to set a message that’s deleted. I figured out that one status column wouldn’t be enough because if 1 use set status to 1 which equals delete then the message would be deleted for both users.
So I decided to create a migration to add sender_status and recipient_status in my messages table and then remove the status column.
I have an update_attribute method in my destro action of my control which updates the specific column when the the delete link is clicked. I will eventually add some more code to destroy the whole message but only when both sender and recipients status’s both equal 1.
My controller action:
def destroy
@message = Message.find(params[:id])
@message.update_attribute('sender_status', 1) if @message.sender_id == current_user.id
@message.update_attribute('recipient_status', 1) if @message.recipient_id == current_user.id
flash[:success] = "Message deleted"
redirect_to :back
end
This helps set either the sender or recipient status. I have this in my controller also:
def show
@current_thread = MessageThread.where('sender_id = ? OR recipient_id = ?', current_user.id, current_user.id).where(:message_id => params[:id]).first
@current_thread_messages = @current_thread.message.children.where(:sender_status => 0, :recipient_status => 0)
end
So as long as status’s for message rows are 0 the message will show. Right now when I delete a message it is still not shown for both users in the conversation thread.
I can’t seem to figure out a clean way to make this delete illusion work. Mayve a status column has to be permanently linked up with the user or maybe I need a separate column for status’s but then things will get messy. That would be an extra table where i’d have to store more message_id’s.
I would really appreciate help from some experts here.
Kind reagrds
For my reading of your code, you’re almost there. As far as I can tell, it’s just this line that’s keeping your new system hung up:
‘Cause that’s forcing the message to not be deleted for both users, and that’s the old behaviour. An uglier but more conditional where call should give you what you want:
Hope that helps!
PS: Apologies for any syntax bugs – I’m still on Rails 2.3, so I’m just guessing that things stayed mostly the same…