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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:31:04+00:00 2026-05-29T09:31:04+00:00

I am a rails newbie and I am trying to create a database schema

  • 0

I am a rails newbie and I am trying to create a database schema that looks like the following:

There are many matches. Each match has 2 teams.

A team has many matches.

The team model and match model are joined together through a competition table.

I have that competition model with a match_id and a team1_id and a team2_id.

But I don’t know how to make this work or if it’s even the best way to go about it. I don’t know how to make certain teams team1 and others team2…. two foreign keys? Is that possible?

The match table also needs to hold additional data like team1_points and team2_points, winner and loser, etc.

  • 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-29T09:31:05+00:00Added an answer on May 29, 2026 at 9:31 am

    You can have as many foreign keys as you want in a table. I wrote an application that involved scheduling teams playing in games.

    The way that I handled this in the Game class with the following:

    class Game < ActiveRecord::Base
      belongs_to :home_team, :class_name => 'Team', :foreign_key => 'team1_id'
      belongs_to :visitor_team, :class_name => 'Team', :foreign_key => 'team2_id'
    

    You can add appropriate fields for team1_points, team2_points, etc. You’ll need to set up your Team model with something like:

    class Team < ActiveRecord::Base
      has_many :home_games, :class_name => 'Game', :foreign_key => 'team1_id'
      has_many :visitor_games, :class_name => 'Game', :foreign_key => 'team2_id'
    
      def games
        home_games + visitor_games
      end
    
      #important other logic missing
    end
    

    Note that some of my naming conventions were the result of having to work with a legacy database.

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

Sidebar

Related Questions

I'm a newbie Rails developer who is getting the following error when trying to
Newbie question, beware! I'd like to implement a basic many-to-many relationship in Rails and
I am newbie trying to learn some code. I am following the tutorial on
Complete rails newbie trying to get started. I have two classes, Ingredient, and Unit.
I'm a rails newbie, I've been trying to figure out what is going on
Bit of a Rails newbie here so please bear with me. I am trying
hey I am newbie to ruby on rails and I am trying run a
Still a rails and ruby newbie, and I'm trying to figure out why adding
I'm using Cancan, Devise, Rails 3 for my ordering application. Each user has many
Rails newbie here. Can anyone please explain the difference to me between the following

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.