SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Kansas City, MO
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Rails: How to implement multiple FK's from same table

    How would I implement in Rails, a model that uses two foreign keys from the same table?

    I am setting up a sports schedule database. I have a team's table that has a PK of id, and then there will be a game's table with two references to the team table, a home team and an away team. Since there are two columns, naming them the conventional way, team_id, won't work.

    What do I need to do to implement this in Rails both on the DB side and within my Games model?

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have not personally done this, but I think you can do something like:

    Code:
    class Game < ActiveRecord::Base
    belongs_to :home,
        :class_name => "Team",
        :foreign_key => "home_team_id"
    belongs_to :away,
        :class_name => "Team",
        :foreign_key => "away_team_id"
    end
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •