2014-02-17 22 views
0

我創建Rails中一個郵件系統,並且爲了顯示交談夥伴選擇屬性,我從CURRENT_USER包含在消息得到他們的ID ,因爲我需要其他用戶從該查詢ID,我需要選擇它,我想選擇使用:to_id:user_id取其等於current_user.id未current_user.id

喜歡的東西:

Message.where("to_id = ? or user_id = ?", current_user.id, current_user.id).select(:to_id, :user_id unless current_user.id) 

預先感謝您!

編輯:這是信息表簡化爲:

create_table "messages", force: true do |t| 
    t.integer "to_id" -> User to whom the message was forwarded to 
    t.integer "user_id" -> User who wrote the message 
    end 
+0

是你只是想獲得一個ID數組,或者你需要知道它們是否來自:to_id和:user_id字段? – blotto

回答

0

,如果你只是想獲得的ID:

Message.where("to_id = ? or user_id = ?", current_user.id, current_user.id). 
     map { |m| 
     m.user_id == current_user.id ? m.to_id : m.user_id 
     } 

這會給你和數組一樣

# => [3771, 3611, 3749, 3638, 3697, 3786] 
+0

是的,只需要擁有該ID沒有所有權。這工作,謝謝你一噸! – Kasperi

相關問題