控制器计数关系中的自引用查找

发布于 2024-11-14 23:31:20 字数 1098 浏览 4 评论 0原文

我在提取一组与用户自引用相关的记录以便在用户的“显示”页面上显示这些记录时遇到了真正的麻烦。

想法如下:

用户 (current_user) 对另外两个用户(user_auser_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生一个溪 2024-11-21 23:31:20

那么我说的对吗,你有 3 个模型

  • User (id, name)
  • PositiveConnection (user_a_id, user_b_id)
  • NegativeConnection (user_a_id, user_b_id)

或类似的东西。

我想你只想要 2 个模型
为了方便起见,我将把关系重命名为“from_user”和“to_user”

  • User (id, name)
  • Connection (value:integer, from_user_id, to_user_id)

其中负数的值为 -1
+1 表示积极。

现在我们可以做类似的事情
(注意:您需要理清确切的语法,例如 :foreign_key 和 :source 等)

class User

  has_many :connections, :foreign_key => "from_user_id"
  has_many :connected_users, :through => :connections, :source => :to_user

  def positive_connections
    connections.where(:value => 1)
  end

  def negative_connections
    ...
  end

end

但是我们现在也有一个框架来创建复杂的 SQL 查询
(同样你需要填补空白......但类似的东西)

class User

  def positive_connected_users
    connected_users.joins(:connections).group("from_user_id").having("SUM(connections.value) > 0")
  end

end

这不太行得通
但是是真正解决方案的伪代码

(用纯sql术语思考可能更好)

SELECT users.* FROM users
INNER JOIN connections ON to_user_id = users.id
WHERE  from_user_id = #{user.id}
HAVING SUM(connections.value) > 0

So am I right in saying you have 3 models

  • User (id, name)
  • PositiveConnection (user_a_id, user_b_id)
  • NegativeConnection (user_a_id, user_b_id)

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"

  • User (id, name)
  • Connection (value:integer, from_user_id, to_user_id)

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)

class User

  has_many :connections, :foreign_key => "from_user_id"
  has_many :connected_users, :through => :connections, :source => :to_user

  def positive_connections
    connections.where(:value => 1)
  end

  def negative_connections
    ...
  end

end

But we also now have a framework to create a complex sql query
(again you need to fill in the blanks... but something like)

class User

  def positive_connected_users
    connected_users.joins(:connections).group("from_user_id").having("SUM(connections.value) > 0")
  end

end

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)

SELECT users.* FROM users
INNER JOIN connections ON to_user_id = users.id
WHERE  from_user_id = #{user.id}
HAVING SUM(connections.value) > 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文