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

  • Home
  • SEARCH
  • 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 8130679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:48:48+00:00 2026-06-06T08:48:48+00:00

Here’s the what I’ve got: I have a model Match and a model Team.

  • 0

Here’s the what I’ve got:

I have a model Match and a model Team. A Match has home_team and away_team. In fact it’s a 2:n relationship.

class Team < ActiveRecord::Base
 has_many :home_matches, :class_name => 'Match', :foreign_key => 'home_team_id'
 has_many :away_matches, :class_name => 'Match', :foreign_key => 'away_team_id'

 public
 def matches
   return home_matches + away_matches
 end
end

and

class Match < ActiveRecord::Base
 attr_accessible :away_team_id, :home_team_id
 belongs_to :home_team, :class_name => 'Team', :foreign_key => 'home_team_id'
 belongs_to :away_team, :class_name => 'Team', :foreign_key => 'away_team_id'

end

Effectively I can now call Team.find(2).matches and get all away and home matches. But what I don’t like is that it needs two SQL queries instead of one:

SELECT `matches`.* FROM `matches` WHERE `matches`.`home_team_id` = 2
SELECT `matches`.* FROM `matches` WHERE `matches`.`away_team_id` = 2

How can I get Rails to use this query?

  SELECT `matches`.* FROM `matches` WHERE `matches`.`home_team_id` = 2 OR `matches`.`away_team_id` = 2

The other way around causes the same headache; If I define a method team that merges home_team and away_team I query the DB two times where only one query is necessary.

  • 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-06T08:48:50+00:00Added an answer on June 6, 2026 at 8:48 am
    def matches
        Match.where("home_team_id = ? OR away_team_id = ?", id)
    end
    

    Also, you don’t need those foreign keys in the Match model. Those attributes are contained in the Match model itself and are therefore not foreign.

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

Sidebar

Related Questions

Here is my problem. I have a LinearLayout that has clickable=true for an onTouch
Here's a Clone() implementation for my class: MyClass^ Clone(){ return gcnew MyClass(this->member1, this->member2); }
Here's my scenario: <!-- Normal Control --> <div class=required> <label for=address1>Address line 1</label> <input
Here's the basic setup: I have a thin bar at the top of a
Here is my problem : I have a post controller with the action create.
Here is an example: I have the generic type called Account. I wish to
Here is the problem that I am trying to solve. I have two folders
Here is my class: public class A{ private void doIt(int[] X, int[] Y){ //change
Here is my code...I have two dimensional matrices A,B. I want to develop the
here's what we have today: * NxM grid of points in 3D * we

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.