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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:31:24+00:00 2026-05-27T18:31:24+00:00

I have a model Country (which is the same as ‘Team’) and a model

  • 0

I have a model Country (which is the same as ‘Team’) and a model Match and I am trying to build a scenario where I have a Match record with both home & away teams.

The models

class Country < ActiveRecord::Base
  has_many :home_matches, :foreign_key => 'home', :class_name => "Match"
  has_many :away_matches, :foreign_key => 'away', :class_name => "Match"
end

class Match < ActiveRecord::Base
  belongs_to :home, :class_name => "Country", :foreign_key => "home"
  belongs_to :away, :class_name => "Country", :foreign_key => "away"
end

The schemas

  create_table "countries", :force => true do |t|
    t.string   "name"
    t.text     "bio"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "matches", :force => true do |t|
    t.datetime "matchdate"
    t.integer  "home"
    t.integer  "away"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

Problem

This works well if I just want:

> canada.away_matches
> japan.home_matches

But how do I get all matches that a country is playing?

Update:

I found the answer in another reply.
ActiveRecord has two association

I have updated my Country model with the following code:

def matches
  Match.where("home = ? OR away = ?", self, self)
end

Now I can query:

> canada.home_matches
> canada.away_matches
> canada.matches

And get the desired result.

  • 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-27T18:31:25+00:00Added an answer on May 27, 2026 at 6:31 pm

    You are setting up the associations in a wrong way.
    Go through this

    1. Single_Table_Inheritance wiki, and
    2. single-table-inheritance-and-where-to-use-it-in-rails
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ItemsContol bound to a Country model - which look like this.
I have a simple model which is just a chain of one-to-many relationships: Country
I have a Class which models a User and another which models his country.
Ok say I have the following model: class Country < ActiveRecord::Base validates_presence_of :name validates_presence_of
I have model Foo which has field bar. The bar field should be unique,
just registered. First question :) If I have in my domain model entity Country
I have a model object which is linked to a list of countries in
I am using CakePHP 2.0 and I have a Model with this 'virtualFields': Country.php:
What I want to do is quite simple: I have a model which I
I have an Address Model which has a ForeignKey to a Contact Model: class

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.