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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:38:22+00:00 2026-05-18T08:38:22+00:00

What is the best way to achieve a has two association with activerecord? I

  • 0

What is the best way to achieve a has two association with activerecord?

I have a Team and Game models. Each Team will have_many games @team.games. A Game will have two teams @game.hosting_team and @game.opposing_team.

I started out with two belongs_to/has_one associations but then @team.games would only return their home games.

The other option I can think of is using a HABTM and use a validator to ensure there are only records. The only thing missing is keeping track of the home team. It seems like I need a has many through association but I’m not exactly sure…

Thanks for your help.

This is an example of how the two has_many associations look. The problem here is I would have to call team.games and team.opponents to get a full list of their games

class Team < ActiveRecord::Base
  has_many :games
  has_many :opponents, :class_name => "Team"#, :foreign_key => ""
end

class Game < ActiveRecord::Base
  belongs_to :team, :class_name => "Team" #, :foreign_key => "team_id"
  belongs_to :opponent, :class_name => "Team" #, :foreign_key => "opponent_id"
end

I’d like something like this but this obviously isn’t how belongs_to works.

class Team < ActiveRecord::Base
  has_many :games
end

class Game < ActiveRecord::Base
  belongs_to :hosting_team
  belongs_to :opposing_team
end

My desired api would look like this.

@team.games # return all games home or away
@game.hosting_team # Team
@game.opposing_team # Team
  • 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-18T08:38:22+00:00Added an answer on May 18, 2026 at 8:38 am

    You can probably still model it with the bt/ho associations, and set up games as an accessor method on the team instead of as an association:

    class Team < ActiveRecord::Base
      def games
        Game.find(:conditions => ["home_team_id = ? OR away_team_id = ?", id, id])
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have always wondered what's the best way to achieve this task. In most
What is the best way to achieve application authorization and entitlement in .Net. Earlier
What is the best way to achieve the result on this page In other
When one is not using methods, what is the best, way to achieve the
The best way to explain my problem is by a example. I have a
I was wondering what would be the best way to achieve a multi-language template
I have two lists that i need to combine where the second list has
I was wondering about the best way to setup to following. I have a
I have two tables, table1 has a entry_ID , entry_date and other entry information.
I have a page with two lists. The list on the left has some

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.