views:

41

answers:

1

I have three models of concern here:

  • User
  • Fight
  • FightPunches
  • Punches

The associations are as follows:

  • User has many fights, foreign_key => 'challenger_id or challengee_id'
  • Fight belongs to challenger, as User
  • Fight belongs to challengee, as User
  • Fight has many fight_punches
  • FightPunches belongs to fight
  • Fight has many punches, through fight_punches
  • FightPunch belongs to Punch
  • FightPunch belongs to User

Key notes:

  • There are three fk's in the FightPunch model: fight_id, punch_id, user_id
  • challenger_id and challengee_id reflect the two users who are fighting in the Fight model.

Here is the challenge. I want to create two associations in the Fight model:

  1. has_many challenger_punches
  2. has_many challengee_punches

The first must grab the records from the Punch model; however, it must only grab those records where Fight.challenger_id = FightPunch.user_id.

Same with #2, but just dealing with the challengee.

A: 

http://rails.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001316 look under supported options, specifically "foreign_key" and "primary_key"

Ben Hughes