2012-04-05 91 views
0

我有我有全都設置爲顯示消息的線程意見如下悄悄話模型回報率 - 悄悄話回覆算法

belongs_to :from_user, class_name: 'User' 
belongs_to :to_user, class_name: 'User' 
belongs_to :thread, class_name: 'Message', foreign_key: 'parent_message_id' 
has_many :replies, class_name: 'Message' 
attr_accessible :subject, :body 
scope :original_message, where("parent_message_id IS NULL") 

,我想提出一個快速回復形式在底部。我完全在大腦中凍結設置to_user的最佳方式,因爲線程中的任何給定消息可能屬於當前用戶或另一端的用戶。有什麼建議?

回答

0

如果只有兩個線程的用戶那麼我們可以假設,無論是from_userto_usercurrent_user,這意味着一個不是current_user是對方。所以:

orig_msg = @thread.messages.original_message 

if orig_msg.from_user == current_user 
    other_user = orig_msg.to_user 
else 
    other_user = orig_msg.from_user 
end 

或者:

other_user = orig_msg.to_user 
other_user = orig_msg.from_user unless orig_msg.from_user == current_user 

或者:

other_user = orig_msg.from_user == current_user ? 
       orig_msg.to_usr : orig_msg.from_user 
+0

將這項工作在模型層? 'def other_user(user) from_user == user? to_user:from_user end' – DVG 2012-04-05 19:57:28

+0

在消息模型中?是啊。 – 2012-04-05 20:05:52

0

我有一個類似的要求,我做了什麼是添加JavaScript來移動回覆textarea基於'回覆'按鈕線程被點擊的位置。

在回覆中,設置到後當前用戶答覆並根據崗位的所有者變量,您可以添加to_user

如果它(在一個論壇等)平坦的談話或像railscasts.com這樣的線程對話,這應該工作。