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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:05:24+00:00 2026-06-03T03:05:24+00:00

Im creating an app using RoR to manage a basketball league. I have two

  • 0

Im creating an app using RoR to manage a basketball league. I have two tables: Teams & Games. The Games table takes two teams using foreign keys and contains the amount scored by each team like so:

Teams and Games

Now, I would like to list all the teams, followed by their win-loss record. I cannot think of an easier way of doing this rather than a foreach loop that counts all the records in the games table that contains the team, and the team has more than the other team. And then again for losses. There has to be an easier way.

Any suggestions?

  • 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-03T03:05:25+00:00Added an answer on June 3, 2026 at 3:05 am

    I agree that it would be best to stay away from loading all the records and counting these in Ruby. Doing it in a SQL query will be much faster.

    def team_stats(team_id)
      # Wins are any game where the team played and scored higher than the other team
      wins = Game.where('(home_team_id = ? AND home_team_score > away_team_score) OR (away_team_id = ? AND home_team_score < away_team_score)', team_id, team_id).count
    
      # The opposite for losses
      losses = Game.where('(home_team_id = ? AND home_team_score < away_team_score) OR (away_team_id = ? AND home_team_score > away_team_score)', team_id, team_id).count
    
      # Ties are not accounted for
    
      return {:wins => wins, :losses => losses}
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating an iOS 5 app using Core Data. I have two entities
I am creating Travel guide app using core data. I have 4 entities CITY
I am creating an app using phonegap+jquery mobile.I have three radio buttons and a
I'm creating a small app using python 2.7 with google app engine and I'm
I am creating a WPF app using MVVM. The app manages tagged documents, called
I'm creating a WPF app using the MVVM design pattern, and I'm trying to
I'm creating an iphone app using xcode 4.2, and trying to use the AVFoundation
I am creating a mobile app using jquery mobile, PHP using JSON for fetching
I've been creating an iPhone App using Core Data. First of all, does it
if yes, is there a tutorial available for creating an android app using the

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.