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 376629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:37:04+00:00 2026-05-12T14:37:04+00:00

Suppose I am doing matchmaking of users and games. I have models containing users

  • 0

Suppose I am doing matchmaking of users and games.
I have models containing users and games.

class Game < ActiveRecord::Base
  has_and_belongs_to_many :users 

class User < ActiveRecord::Base
  has_and_belongs_to_many :games

Games can have many users, users can be playing many games. Because of HASBM I have a table called games_users too.

I want to search and find games that are waiting for players, which do not also contain the username of the player (i.e. I don’t want to add the same player to a game twice…)

I want something like this:

@game = Game.find_by_status(Status::WAITING_USERS, :condition => “game.users.doesnt_contain(‘username=player’)

But I’m not sure how to do it?

Update:

Using jdl’s solution, I got the code to run, but get items that I tried to exclude returned in the results. Here’s my test code:

logger.debug "Excluding user: #{@user.id}"
games = Game.excluding_user(@user)
if (games != nil && games.count > 0)
  @game = Game.find(games[0].id)
  games[0].users.each {
   |u|
   logger.debug "returned game user: #{u.id}"
  }
end

(the above code also begs 2 questions…. – how do I get a result of just one game instead of an array, and how to I get a non-readonly version of it; that’s why I do the second Game.find…)

And here’s the output in the log:

Excluding user: 2
  Game Load (0.3ms)   SELECT `games`.* FROM `games` left outer join games_users gu on gu.game_id = games.id WHERE (gu.game_id is null or gu.user_id != 2) 
  Game Columns (1.0ms)   SHOW FIELDS FROM `games`
  SQL (0.2ms)   SELECT count(*) AS count_all FROM `games` left outer join games_users gu on gu.game_id = games.id WHERE (gu.game_id is null or gu.user_id != 2) 
  Game Load (0.1ms)   SELECT * FROM `games` WHERE (`games`.`id` = 3) 
  games_users Columns (6.8ms)   SHOW FIELDS FROM `games_users`
  User Load (0.9ms)   SELECT * FROM `users` INNER JOIN `games_users` ON `users`.id = `games_users`.user_id WHERE (`games_users`.game_id = 3 ) 
returned game user: 1
returned game user: 2
  • 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-12T14:37:04+00:00Added an answer on May 12, 2026 at 2:37 pm

    Named scopes are your friend here.

    For example:

    class Game < ActiveRecord::Base
      has_and_belongs_to_many :users
    
      named_scope :for_status, lambda {|s| {:conditions => {:status => s}}}
      named_scope :excluding_user, lambda {|u| {:conditions => ["gu.game_id is null or gu.game_id not in (select game_id from games_users where user_id = ?) ", u.id], :joins => "left outer join games_users gu on gu.game_id = games.id", :group => "games.id" }}
    end
    

    This will let you do things like the following:

    user = User.first  # Or whoever.
    games_in_progress = Game.for_status("playing")
    games_in_progress_for_others = Game.excluding_user(user).for_status("playing")
    # etc...
    

    Also, since you say that you’re new to Rails, you might not realize that these named scopes will also work when you’re traversing associations. For example:

    user = User.first
    users_games_in_waiting = user.games.for_status("waiting")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a script: my_script.sh Instead of doing ./my_script.sh I want to do
Hi guys I have the following VHDL which isn't doing what it suppose to
Suppose I have a byte[] array, I am wondering what I am really doing
Im doing a class for polynomial and i have a problem with a copy
Suppose I have a Utility class, public class Utility { private Utility() {} //Don't
Suppose I have this: class external { JFrame myFrame; ... class internal implements ActionListener
Suppose I have two models A and B Now I want to copy some
I'm doing a module where a user suppose to assign into a specific group.
Suppose I want to determine if Admin inherits from ActiveRecord::Base . One way is
Suppose I'm doing some iPhone development and I have a subclass of UIViewController .

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.