我试图创建一个应用程序,用户('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