我在Rails 4中構建了一個社交網站。用戶可以請求關注另一個用戶。Rails 4關係狀態方法,這是最優的嗎?
在我的用戶模型中,我有以下方法。它的工作原理,但我想知道是否是確定關係狀態的最好方法。這個比例可以嗎?
謝謝。
def relationship_status(user_2)
relationship = Relationship.where(follower_id: self.id, followed_id: user_2.id)
unless relationship.any?
relationship = Relationship.where(followed_id: self.id, follower_id: user_2.id)
unless relationship.any?
return "not_friends"
else
if relationship.first.status == "pending"
return "pending_recieved"
elsif relationship.first.status == "ignored"
return "you_ignored"
elsif relationship.first.status == "active"
return "your_followed"
end
end
else
if relationship.first.status == "pending"
return "pending_sent"
elsif relationship.first.status == "ignored"
return "your_ignored"
elsif relationship.first.status == "active"
return "you_are_following"
end
end
end
我想你在你的代碼中有一個錯誤。你定義的兩個關係都是一樣的! – fny
where子句被顛倒過來。 follower_id和followed_id。 –