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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:32:36+00:00 2026-06-01T00:32:36+00:00

trying to refactor code to provide clean association A GAME has a HOME_TEAM and

  • 0

trying to refactor code to provide clean association

A GAME has a HOME_TEAM and an AWAY_TEAM

A TEAM has many GAMES as a HOME_TEAM or an AWAY_TEAM

Association between GAME and TEAM is a straight-forward HABTM BUT I need to denote which of the two TEAMS associated with a GAME is the HOME_TEAM and which is the AWAY_TEAM. I did it by adding extra fields and associations but this is obvious very wet rather than dry. I know the answer is in through but I seem to have had a brain meltdown and can’t quite get my head round this.

Basically I want to be able to do Game.teams (returns collection of both teams) and Game.home_team (get and set a TEAM to home_team) and Game.away_team (get and set a TEAM to away_team)

Sorry to pose such a straightforward sounding query but it’s just got away from me

class Game < ActiveRecord::Base
  belongs_to :home_team
  belongs_to :away_team
  has_and_belongs_to_many :teams
end

class HomeTeam < ActiveRecord::Base
  belongs_to :team
  has_one :games
end

class AwayTeam < ActiveRecord::Base
  belongs_to :team
  has_one :games
end

class Team < ActiveRecord::Base
  has_and_belongs_to_many :games
  has_many :away_teams
  has_many :home_teams
end 

All help greatly appreciated

Peter

  • 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-06-01T00:32:38+00:00Added an answer on June 1, 2026 at 12:32 am

    To do what you want to do, you should use a has_many :through instead of hatbm. See here for more info. In short, the good thing is that you can add other variables to the joins table. In your case, a boolean called home_team.

    So here’s what I’d do. First, create an association table (since I don’t have much imagination, I’ll call it participation):

    create_table :participations, do |t|
      t.integer :game_id, :null => false
      t.integer :team_id, :null => false
      t.boolean :home_team
    end
    

    As you can see, unlike your gamesteams table, this one has an id. And you can add attributes to it.
    Then, I would use these models:

    class Participation < ActiveRecord::Base
      belongs_to :game
      belongs_to :team
    end
    
    class Game < ActiveRecord::Base
      has_many :participations, :dependent => :destroy
      has_many :teams, :through => :participations
    end
    
    class Team < ActiveRecord::Base
      has_many :participations, :dependent => :destroy
      has_many :games, :through => :participations
    end
    

    So to get the teams of a game, you do @game.teams.

    Now, to get home_team and away_team, add these methods to your Game model:

    def home_team
      self.teams.joins(:participations).where("participations.home_team IS ?", true).first
    end
    
    def away_team
      self.teams.joins(:participations).where("participations.home_team IS ?", false).first
    end
    

    And then you’ll be able to do @game.home_team and @game.away_team.

    Peter’s edit: Ok, so for mysql you’ll have to use different where
    statements:

    self.teams.joins(:participants).where(“participants.home_team = ?”, true).first
    self.teams.joins(:participants).where(“participants.home_team IS NULL”).first

    I can either use ” = ?”, true and “!= ?”, true –OR– IS NOT NULL and
    IS NULL

    I think for false you should try using where("participants.home_team = ?", false)

    Ok, so so there are at least 2 ways to set up your teams.

    1. You let the user pick which team is playing home
    2. You assume the first team is the home team

    If you go for number 1, you should use a radio button to let the user decide. Something like this:

    <%= label_tag :home, 'Home Team' %><br />
    <%= label_tag :home_team_1, 'Team 1' %><%= radio_button_tag :home_team, 1 %>
    <%= label_tag :home_team_2, 'Team 2' %><%= radio_button_tag :home_team, 2 %>
    

    So if params[:home_team] == 1, the first team is the home team, if params[:home_team] == 2, the second team is the home team.

    If you go for number 2, then, you should have something like this in your form do add the teams to your game:

      <%= label_tag :name, 'Home Team' %>
      <%= text_field_tag :name, nil, :name => "home[]" %>
    
      <%= label_tag :name, 'Away Team' %>
      <%= text_field_tag :name, nil, :name => "away[]" %>
    

    So then in your controller you can do something like

    @game = Game.new(params[:game])
    
    home = Team.create(params[:home])
    # or
    home = Team.find_or_create_by_name(params[:home][:name])
    @game.participations.create(:team_id => home.id, :home_team => true or 1)
    
    away = Team.find_or_create_by_name(params[:away][:name])
    @game.participations.create(:team_id => away.id, :home_team => false or 0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code for adding to/extracting from Zip. I'm trying to refactor
I am trying to refactor some code in an ASP.Net website and having a
I am trying to refactor some code while leaving existing functionality in tact. I'm
I'm trying to refactor some code and move some of my before_filter's from the
I am trying to refactor a working code. The code basically derives an interface
I'm quite new to smart pointers and was trying to refactor some existing code
I am trying to refactor my code so that it follows good OO pattern,
I'm trying to refactor some legacy code to use Spring to handle the jms
I'm new to javascript and was trying to refactor some code, clearly there's something
I am trying attempting to refactor the following code: public static void SaveSplitbar(RadSplitBar splitBar)

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.