0

好吧破壞紅寶石一排軌道上針對特定用戶的我有一個消息表:如何在對話線程

+-----+-----------+--------------+--------------+-----------+--------+--------------+--------------+ 
| 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... | 
+-----+-----------+--------------+--------------+-----------+--------+--------------+--------------+ 

我試圖找出一種方法來設置的已刪除的消息。我發現只有一個狀態列是不夠的,因爲如果將set狀態設置爲1(等於delete),那麼對於兩個用戶都將刪除該消息。

因此,我決定創建一個遷移,在我的消息表中添加sender_status和recipient_status,然後刪除狀態列。

我在我的控制器的destro動作中有一個update_attribute方法,它在點擊刪除鏈接時更新特定的列。我最終會添加一些更多的代碼來破壞整個消息,但只有當發送者和接受者地位的都等於1

我的控制器動作:

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 

這有助於設置發送者或接收者的地位。我在我的控制器也有這個:

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 

所以只要消息行的狀態是0,消息就會顯示。現在,當我刪除一條消息時,它仍然不會在會話線程中顯示給兩個用戶。

我似乎無法找出一個乾淨的方式,使這種刪除錯覺的工作。 Mayve狀態列必須與用戶永久連接,或者我需要一個單獨的狀態列,但事情會變得混亂。這將是一個額外的表,我不得不存儲更多的message_id。

我真的很感謝這裏的一些專家的幫助。 Kind reagrds

+0

雙崗豪斯先生:( 的http:// stackoverflow.com/questions/9327652/updating-more-than-1-atribute-at-one-time-in-ruby-on-rails/9328724#9328724 – TomDunning 2012-02-17 13:05:56

回答

2

對於我的閱讀你的代碼,你幾乎在那裏。據我所知,這只是這條線那就是維持你的新系統就掛了:

@current_thread_messages = @current_thread.message.children.where(
    :sender_status => 0, 
    :recipient_status => 0 
) 

因爲那真實逼用戶不會被刪除的消息,這是舊的行爲。一個醜陋的,但更有條件的地方,呼籲應該給你你想要的:

@current_thread_messages = @current_thread.message.children.where([ 
    '(sender_id = ? AND sender_status = 0) OR (recipient_id = ? AND recipient_status = 0)', 
    current_user.id, 
    current_user.id 
]) 

希望幫助!

PS:對於任何語法錯誤道歉 - 我仍然on Rails的2.3,所以我只是猜測,事情大多停留在同一...

+0

謝謝,工作完美,所有的權力都在這一行。 – LondonGuy 2012-02-16 22:42:55