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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:53:16+00:00 2026-05-25T20:53:16+00:00

My models class Team < ActiveRecord::Base has_many :team_players has_many :players, :through => :team_players end

  • 0

My models

class Team < ActiveRecord::Base
  has_many :team_players
  has_many :players, :through => :team_players
end

class TeamPlayer < ActiveRecord::Base
  belongs_to :team
  belongs_to :player
end
  • Teams can have a different number of players
  • Players can be on many teams
  • Retrieving a team_id if it already exists <- My problem

When creating a new team, I’m basically choosing multiple players in a nested form. Upon create, I need to check if a team with the exact player composition already exist.

Example:

  • Team 1: A (id 1) and B (id 2)
  • Team 2: A (id 1), B (id 2) and C (id 3)

Lets say I create a new team with A and B, then I need the query somehow tell me if it exists or not, by returning the team_id or something, or an empty result.

I’ve played around with queries similar to this, but with no satisfying result

SELECT *, count(id) as c FROM "team_players" WHERE player_id IN (1,3) GROUP BY team_id HAVING c = 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-25T20:53:17+00:00Added an answer on May 25, 2026 at 8:53 pm

    I think you can do this in two steps:

    1. Find the teams that have all the players you’re looking at.
    2. Check if those teams have more players than the player set you’re looking at.

    That translates into this:

    select team_id
    from team_players
    where team_id in (
        select team_id
        from team_players
        where player_id in (1,3)
        group by team_id
        having count(player_id) = 2
    )
    group by team_id
    having count(player_id) = 2
    

    Given this in team_players:

    team_id|player_id
         1 | 1
         1 | 3
         1 | 4
         2 | 1
         2 | 3
         3 | 3
    

    The above query says team_id = 2 and there’s your exact match. The query you’re using gives you the teams that contain the players in question as a subset, not the teams that are set-wise equal to the players in question.

    If you only want to know if such a team exists then wrap that query in a select_rows call and you’re done:

    class Team < ActiveRecord::Base
        # players is an array of Fixnum player IDs
        def self.team_exist?(players)
            return false if(!players.present?)
            connection.select_rows(%Q{
                select team_id
                from team_players
                where team_id in (
                    select team_id
                    from team_players
                    where player_id in (#{players.join(',')})
                    group by team_id
                    having count(player_id) = #{players.length}
                )
                group by team_id
                having count(player_id) = #{players.length}
            }).present?
        end
    end
    

    The #{players.join(',')} interpolation assumes that team_exist? is being given an array of Fixnum so you’ll want to properly scrub your data in the caller or add a players = players.map(&:to_i) before the connect.select_rows call to scrub it inside team_exist?.

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

Sidebar

Related Questions

module App::Models class Team < Base has_many :players [...] end class Player < Base
I currently have the following models: class Player < ActiveRecord::Base belongs_to :team belongs_to :user
I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through =>
I have the following models: class Person < ActiveRecord::Base has_many :accounts, :through => :account_holders
I have the following models: class FieldEntryValue < ActiveRecord::Base belongs_to :field_entry end and class
I have 3 models. class Team < ActiveRecord::Base ... has_many :seasons_teams, :dependent => :destroy
I have the following models: class Match < ActiveRecord::Base has_and_belongs_to_many :teams end And class
I've got 3 models: User, Team, and Membership - class Team < ActiveRecord::Base has_many
my models class Auction belongs_to :item belongs_to :user, :foreign_key => :current_winner_id has_many :auction_bids end
I want to associate two models (Team and Member) by has_many :through, but returns

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.