Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6019429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:25:13+00:00 2026-05-23T03:25:13+00:00

I’m trying to create an app where users (‘current_user’) rate the compatibility between two

  • 0

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.

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.

This is my problem: on every user (@user) page I want to display separate lists of:

  1. users that are overall_positively_connected_to(@user) (i.e. positive_connections.count > negative_ratings.count), and

  2. users that are overall_negatively_connected_to(@user) (i.e. negative_connections.count > positive_ratings.count).

What I can’t seem to do is write a class method that pulls out only those users who are net-rated “compatible” or “incompatible”

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:

class User < ActiveRecord::Base


def self.that_are_compatible_with

     User.overall_positively_connected_to(user)
end
.
.
.

EDIT

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):

.
.
.
  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

But that’s not much help. What I need is a method that gets the users overall_positively_connected_to(user). I presume the method would involve two joins, and go something like this:

  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

But here I get completely stuck: it’s obviously not right. I can’t find other examples like it anywhere…

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:25:14+00:00Added an answer on May 23, 2026 at 3:25 am

    Having struggled with this for a few days, I decided that the best way of going about solving the problem is probably to change the models round – either by adding an additional “connection” model and having users vote positively or negatively on each connection, or by slimming down to a single “connection” model where the positive or negative character of each connection is marked by a +1/-1.

    A couple of alternatives can be found in answers to this question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.