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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:25:03+00:00 2026-06-05T16:25:03+00:00

Relatively new to rails, I’m working on a simple rails app for a few

  • 0

Relatively new to rails, I’m working on a simple rails app for a few friends to handicap nfl games.

I’m creating an active record model that models information about a sports game, such as home team, visitor, start_time, etc, as well as information related to handicapping that game (favorite, underdog, total, etc).

This feels awkward, becuase for several of these fields I’m basically storing duplicate information (For example, a team is always either a home_team or visitor, and always either a favorite or underdog)

So, my model would look something like this:

create_table "games", :force => true do |t|
    t.integer  "week"
    t.string   "home_team"
    t.string   "visitor_team"
    t.string   "favorite"
    t.decimal  "line"
    t.decimal  "total"
    t.string   "kickoff"
    t.string   "status"   #['open', 'completed']
    t.datetime "created_at",   :null => false
    t.datetime "updated_at",   :null => false
end

To keep the model as light as possible I defined only the required fields (home team, visitor and favorite, total) and then I wrote a few simple method in my Game model to return other useful info about the matchup, e.g. instead of listing each team twice in the model (once for underdog/favorite and home/away)

def underdog
    teams = [self.home_team, self.visitor_team]
    teams.each do |team|
        return team if team != self.favorite
    end
end

This works, but I was debating whether it made more sense to either create a seperate model for game information (and keep the game model itself minimal) and just use has_many => :through or try some other approach because they way I have my model set up doesn’t seem to feel right storing duplicate information in the same table. Any suggestions or appreciated.

  • 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-05T16:25:05+00:00Added an answer on June 5, 2026 at 4:25 pm

    This looks fine to me. In general, you should avoid using has-many-through relationships because they’re messy and complicated and most often unnecessary.

    In this case, though, I’d actually recommend creating a Team model. Since it’s the NFL, these teams won’t ever change, but pulling it out lets you reduce the number of strings you need to fill in the database.

    I’d also refactor the original method:

    def underdog
        if home_team == favorite_team
            home_team
        else
            visitor_team
        end
    end
    

    In Ruby, you do not need to explicitly call return, as the last statement in your method is implicitly returned. Also, using a loop in this manner in Ruby would be considered bad form.

    Also, you should take a look at this article on when to use self in a model method.

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

Sidebar

Related Questions

I'm relatively new to Rails and busy building an app with various access levels,
I've got a relatively simple Rails app that takes a form with nested params
Relatively new to rails and trying to model a very simple family tree with
I am relatively new to rails and have been working my way through the
I'm relatively new to Ruby on Rails. As of now have this working code
I'm creating a basic sign up form using ruby on rails (I'm relatively new
Ok, I'm relatively new to rails and am working through the book Rails for
There are several stages to this, and as I am relatively new to rails
I'm relatively new to RoR and I'm curious about why Rails compiles assets both
I'm relatively new to working with XML and am working with some rather large

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.