控制器计数关系中的自引用查找 [英] Self-referential find in controller count relations

查看:292
本文介绍了控制器计数关系中的自引用查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在用户的显示页面上显示这些记录,我很难拔出一组与用户自相关的记录。



<用户( current_user )评价两个其他用户之间的兼容性(<$ c $> c> user_a 和 user_b )。他们可以对兼容性进行正面或负面评价:对两个用户进行兼容评级,在user_a和user_b之间创建一个 positive_connection ,并将它们评为不兼容,创建一个 negative_connection 。因此,有positive_connection,negative_connection和用户的模型。$ ​​b
$现在我只需要显示 overall_positively_connected_to(@user)



positive_connections_to



用户模式:



这是我需要的地方,但是我不能进一步:

  def overall_positive_connected_to(user)
positive_connections_to(user).count> negative_connections_to(user).count
end

$ b 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),所以可能做了一些天真的事。



任何帮助都会很棒。




  • 用户(id ,名称)

  • PositiveConnection(user_a_id,user_b_id)

  • NegativeConnection(user_a_id,user_b_id)


或类似的东西。



我想你只需要2个模型
,为了方便,我打算将关系重命名为from_user和to_user
$ b


  • 用户(id,name)

  • 连接(value:integer,from_user_id,to_user_id)


    其中值为-1的负
    和+1正确。

    现在我们可以做一些类似于
    的事情(注意:你需要清楚确切的语法,例如:foreign_key,:source和stuff )

      class User 
    $ b $ has_many:connections,:foreign_key => from_user_id
    has_many:connected_users,:through => :connections,:source => :to_user
    $ b def positive_connections
    connections.where(:value => 1)
    end

    def negative_connections
    ...
    end

    end

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

    $ $ p $ $ $ $ $ $ $用户

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

    end

    这并不完全符合
    的要求,是一种真正的解决方案的伪代码



    (这可能是更好地思考在纯SQL语言)

     SELECT用户。* FROM用户
    INNER JOIN连接ON to_user_id = users.id
    WHERE from_user_id =#{user.id}
    HAVING SUM .value)> 0


    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.

    解决方案

    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
    

    这篇关于控制器计数关系中的自引用查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆