Created
July 1, 2011 02:21
-
-
Save leviwilson/1057757 to your computer and use it in GitHub Desktop.
RoR Associations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#rails gen model Player name:string | |
class Player < ActiveRecord:Base | |
has_many :wins, :class_name => "Game", :foreign_key => "winner" | |
has_many :losses, :class_name => "Game", :foreign_key => "loser" | |
end | |
#rails gen model Game winner:integer loser:integer | |
class Game < ActiveRecord:Base | |
belongs_to :winner, :class_name => "Player", :foreign_key => "player_id" | |
belongs_to :loser, :class_name => "Player", :foreign_key => "player_id" | |
end |
Nevermind, looks like I had the Game record setup horribly :-) Here is what worked (I haven't tested "wins" and "losses" on the Player model yet)
class Game < ActiveRecord:Base
belongs_to :player, :foreign_key => "winner"
belongs_to :player, :foreign_key => "loser"
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this a completely wrong approach? It seems like I should be able to do this.