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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:40:07+00:00 2026-06-01T11:40:07+00:00

I’m working on a Rails-based web service that provides data about various sports team

  • 0

I’m working on a Rails-based web service that provides data about various sports team schedules. I have models that included the following:

  • Each Game record has a “home” and an “away” team, each of which references a Team record
  • Each Team record belongs to a Division record

I have modeled Game as follows:

class Game < ActiveRecord::Base

  # Miscellaneous validations here

  belongs_to :home_team, :class_name => Team
  belongs_to :away_team, :class_name => Team

  # Other stuff follows

end

Here is the model for Team:

class Team < ActiveRecord::Base

  # Miscellaneous validations here

  belongs_to :division

  # Other stuff follows

end

And here is the model for Division:

class Division < ActiveRecord::Base

  # Miscellaneous validations here

  has_many :teams, :dependent => :destroy

  # Other stuff follows

end

I am trying to implement a request to return all games where the home team and the away team are both from a specific division. In pseudo-code , I want something like:

SELECT games.* FROM games WHERE 
    "The division ID of the home team" = '1' AND
    "The division ID of the away team" = '1' 

I’ve tried various incarnation using the joins method, none of which have worked for me. The closest I’ve come is this:

games = Game.joins(:home_team, :away_team).where(
    :home_team => {:division_id => params[:division_id]}, 
    :away_team => {:division_id => params[:division_id]})

but this gives me an error:

  SQLite3::SQLException: no such column: home_team.division_id: SELECT "games".* FROM "games" INNER JOIN "teams" ON "teams"."id" = "games"."home_team_id" INNER JOIN "teams" "away_teams_games" ON "away_teams_games"."id" = "games"."away_team_id" WHERE "home_team"."division_id" = '1' AND "away_team"."division_id" = '1'

Clearly, my syntax for specific home_team and away_team isn’t working because it’s not mapping them to a valid table name of “teams”. But any other variant of the join I come up with seems to get me even further from what I want.

I’d appreciate any help you can provide or references to documentation that shows me how to do this kind of thing.

  • 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-01T11:40:08+00:00Added an answer on June 1, 2026 at 11:40 am

    I think you might be able to try something like this:

    Game.where(["(SELECT COUNT(DISTINCT teams.id) FROM teams WHERE teams.division_id = ? AND (teams.id=games.home_team_id OR teams.id=games.away_team_id)) = 2", params[:division_id]])
    

    You have the nested query, which is a little annoying, but it lets you avoid having to join twice against the teams table.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.