控制器计数关系中的自引用查找
我在提取一组与用户自引用相关的记录以便在用户的“显示”页面上显示这些记录时遇到了真正的麻烦。
想法如下:
用户 (current_user
) 对另外两个用户(user_a
和 user_b
)之间的兼容性进行评分。他们可以对兼容性进行正面或负面评价:将两个用户评价为“兼容”会在 user_a 和 user_b 之间创建一个正面连接
,而将他们评价为“不兼容”则会创建一个负面连接
。所以有正连接、负连接和用户的模型。
现在,我需要仅显示 overall_positively_connected_to(@user)
的用户(即 positive_connections_to(@user).count > negative_connections_to(@user).count)
。
这是我必须要做的,但我无法进一步:
用户模型:
def overall_positive_connected_to(user)
positive_connections_to(user).count > negative_connections_to(user).count
end
def positive_connections_to(user)
positive_connections.where("user_b_id = ?", user)
end
def negative_connections_to(user)
negative_connections.where("user_b_id = ?", user)
end
控制器
@user.user_bs.each do |user_b|
if user_b.overall_pos_connected_to(@user)
@compatibles = user_b
end
end
控制器中的代码显然是错误的,但我应该如何去做呢?我对rails(和sql)完全陌生,所以可能做了一些幼稚的事情。
任何帮助都会很棒。
I'm having real trouble pulling out a set of records that are self-referentially related to a user in order to show these on a user's 'show' page.
Here's the idea:
Users (current_user
) rate the compatibility between two other users (user_a
and user_b
). They can rate compatibility either positively or negatively: rating two users "compatible" creates a positive_connection
between user_a and user_b, and rating them "incompatible" creates a negative_connection
. So there are models for positive_connection, negative_connection and user.
Now I need to display only users that are overall_positively_connected_to(@user)
(i.e. where positive_connections_to(@user).count > negative_connections_to(@user).count)
.
This is where I've got to, but I can't get any further:
User model:
def overall_positive_connected_to(user)
positive_connections_to(user).count > negative_connections_to(user).count
end
def positive_connections_to(user)
positive_connections.where("user_b_id = ?", user)
end
def negative_connections_to(user)
negative_connections.where("user_b_id = ?", user)
end
Controller
@user.user_bs.each do |user_b|
if user_b.overall_pos_connected_to(@user)
@compatibles = user_b
end
end
The code in the controller is clearly wrong, but how should I go about doing this? I'm completely new to rails (and sql), so may have done something naive.
Any help would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么我说的对吗,你有 3 个模型
或类似的东西。
我想你只想要 2 个模型
为了方便起见,我将把关系重命名为“from_user”和“to_user”
其中负数的值为 -1
+1 表示积极。
现在我们可以做类似的事情
(注意:您需要理清确切的语法,例如 :foreign_key 和 :source 等)
但是我们现在也有一个框架来创建复杂的 SQL 查询
(同样你需要填补空白......但类似的东西)
这不太行得通
但是是真正解决方案的伪代码
(用纯sql术语思考可能更好)
So am I right in saying you have 3 models
Or something of that sort.
I think you just want 2 models
and for convenience I'm going to rename the relations as "from_user" and "to_user"
Where value is -1 for a negative
and +1 for a positive.
Now we can have do something like
(note: you need to sort out the exact syntax, like :foreign_key, and :source, and stuff)
But we also now have a framework to create a complex sql query
(again you need to fill in the blanks... but something like)
this isn't quite going to work
but is kind of pseudo code for a real solution
(it might be better to think in pure sql terms)