Note that there are some explanatory texts on larger screens.

plurals
  1. POClass method problem with bidirectional self-referential associations
    text
    copied!<p>I'm trying to create an app where users ('current_user') rate the compatibility between two other users ('user1' and 'user2'). They can rate compatibility either positively or negatively: rating two users "compatible" creates two resources of the same class (a 'positive_connection' and an 'inverse_positive_connection' - for bidirectionality), and rating them "incompatible" also creates two resources('negative_connection' and 'inverse_negative_connection'). So there are models for positive_connection, negative_connection and user.</p> <p>Each rating resource belongs_to the user who created it, but also to the users connected by it. It's important to have both positive and negative ratings.</p> <p>This is my problem: on every user (<code>@user</code>) page I want to display separate lists of:</p> <ol> <li><p>users that are <code>overall_positively_connected_to(@user)</code> (i.e. <code>positive_connections.count &gt; negative_ratings.count</code>), and </p></li> <li><p>users that are <code>overall_negatively_connected_to(@user)</code> (i.e. <code>negative_connections.count &gt; positive_ratings.count</code>).</p></li> </ol> <p><br /> What I can't seem to do is write a <em>class</em> method that pulls out only those users who are net-rated "compatible" or "incompatible"</p> <p>From reading Michael Hartl's rails tutorial (I'm completely new to all this), I think I need to write something like this in the User model:</p> <pre><code>class User &lt; ActiveRecord::Base def self.that_are_compatible_with User.overall_positively_connected_to(user) end . . . </code></pre> <p><br /></p> <p><strong>EDIT</strong></p> <p>Starting with absolutely no knowledge of SQL queries at all, I wrote these two class methods for finding users that are negatively and positively connected to @user (respectively):</p> <pre><code>. . . def self.with_neg_connections_to(user) joins(:negative_connections). where(:negative_connections =&gt; {:user_a_id =&gt; user}) end def self.with_pos_connections_to(user) joins(:positive_connections). where(:positive_connections =&gt; {:user_a_id =&gt; user}) end </code></pre> <p><br /></p> <p>But that's not much help. What I need is a method that gets the users <code>overall_positively_connected_to(user)</code>. I presume the method would involve two joins, and go something like this:</p> <pre><code> def self.overall_positively_connected_to(user) joins(:positive_connections).joins(:negative_connections). where((:negative_connections =&gt; {:user_a_id =&gt; user}).count &gt; (:positive_connections =&gt; {:user_a_id =&gt; user}).count) end </code></pre> <p><br /> But here I get completely stuck: it's obviously not right. I can't find other examples like it anywhere...</p> <p>Any help whatsoever on this would be great as I have no clue when it comes to SQL queries. Let me know if more code needed. Thanks in advance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload