简体   繁体   中英

Complex Rails associations question

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.

相关文档位于受支持的选项下,尤其是“ foreign_key”和“ primary_key”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM