Question

I have 3 models, Person, Player and Injury.

Player = #<Player id: 9999, name: "Bob the player", person_key: "some-unique-key">

Person = #<Person id: 1234, person_key: "some-unique-key">

Injury = #<Injury id: 8374, injury: "Hurt", person_id: 1234>


Player - has_one :person, :foreign_key => :person_key, :primary_key => :person_key

Person - has_one :player, :foreign_key => :person_key, :primary_key => :person_key

Injury - belongs_to :person


I primarily work with Player and I need to be able to have a has_many relationship from Player to injuries

Player.injury needs to do something akin to;

Player - has_many :injuries, :foreign_key => :person_id, :primary_key => { self.person.id }

I realize that you can't do self.person.id there, but that's the relation I need. (I didn't design this relationship, it's just what I have to work with..)

Thoughts?

Was it helpful?

Solution

You should add folowing relations:

Person - has_many :injuries
Player - has_many :injuries, through: :person
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top