我試圖創建一個應用程序,用戶('current_user')對兩個其他用戶('user1'和'user2')之間的兼容性進行評分。他們可以對兼容性進行正面或負面的評價:對兩個用戶進行「兼容」評級,創建同一類別的兩個資源('positive_connection'和'inverse_positive_connection' - 雙向性),並將它們評爲「不兼容」還會創建兩個資源('negative_connection '和'inverse_negative_connection')。所以有positive_connection,negative_connection和user的模型。雙向自我指涉關聯的類方法問題
每個評級資源都屬於創建它的用戶,但也屬於其所連接的用戶。正面評價和負面評價都很重要。
這是我的問題:對每個用戶(@user
)頁我想顯示的單獨列表:是overall_positively_connected_to(@user)
(即positive_connections.count > negative_ratings.count
)
用戶,
是overall_negatively_connected_to(@user)
用戶(即
negative_connections.count > positive_ratings.count
)。
我似乎無法做的是寫一個類方法翻出只有誰是這些用戶網評爲「兼容」或「不兼容」
從閱讀邁克爾·哈特爾的軌教程(我完全新的這一切),我想我需要寫像這樣在用戶模式:
class User < ActiveRecord::Base
def self.that_are_compatible_with
User.overall_positively_connected_to(user)
end
.
.
.
編輯
絕對沒有在所有SQL查詢的知識開始,我寫了查找用戶這兩個類方法被負,正(分別)連接到@user:
.
.
.
def self.with_neg_connections_to(user)
joins(:negative_connections).
where(:negative_connections => {:user_a_id => user})
end
def self.with_pos_connections_to(user)
joins(:positive_connections).
where(:positive_connections => {:user_a_id => user})
end
但這並沒有太大的幫助。我需要的是一種獲取用戶overall_positively_connected_to(user)
的方法。我相信該方法會涉及兩個連接,並且去是這樣的:
def self.overall_positively_connected_to(user)
joins(:positive_connections).joins(:negative_connections).
where((:negative_connections => {:user_a_id => user}).count > (:positive_connections => {:user_a_id => user}).count)
end
但在這裏,我得到完全被卡住:這顯然是不正確的。我找不到其他例子,像任何地方...
任何幫助什麼這將是偉大的,因爲我不知道什麼時候談到SQL查詢。讓我知道是否需要更多的代碼。提前致謝!
感謝您的答覆澳大利亞。這並不完全是我的意思,但是這使得數據庫結構過於複雜是我的錯。實際上,我想從數據庫中退出那些對任何給定用戶(@user)有更積極或負面聯繫的用戶。最後我通過改變模型解決了這個問題。我將很快發佈我的解決方案作爲答案。無論如何,再次感謝。 – jonic 2011-06-14 09:01:14