daveomcd
daveomcd

Reputation: 6555

How can I have a relationship between models based off an attributes value?

I have the three following models...

Player

id:integer
first_name:string
last_name:string

School

id:integer
short_name:string
long_name:string

SchoolRosterSlot

id:integer
season:integer
jersey:string
position:string
active:boolean

Originally I was going to have jersey, position, school_id stored on the Player model. However, I changed that because I thought it would be redundant to enter that information into the Player model, and also insert it in the SchoolRosterSlot model since I'm wanting to store season information on players. (the players postiion, jersey, and school can all change from season-to-season).

My question is how do I set up my associations so that I can access the player's school via @player.school for instance? or @player.jersey... or is it better to set up a direct association between school and player and just insert the roster slot record also even though this would be recording the information twice?

Upvotes: 0

Views: 38

Answers (1)

GMA
GMA

Reputation: 6096

In general you want to avoid storing duplicate information whenever possible.

You could try adding school_roster_slot_id to Player and school_id to SchoolRosterSlot, then adding this to the models:

class SchoolRosterSlot
  belongs_to :school
end

class Player
  belongs_to :school_roster_slot
  has_one :school, through: :school_roster:slot
end

This will create a player.school method. If you want to also get player.jersey and player.position you could also add this to Player:

delegate :position, :jersey, to :school_roster_slot

This is effectively the same as creating the following two methods:

def jersey
  school_roster_slot.jersey
end

def position
  school_roster_slot.position
end

I don't know if you want to store historical information about player's positions and jerseys (e.g. when a player changes position and jersey, do you still want to store information about their old positions?), but if that's the case you might need to do something like creating an additional model called Season that sits in between Player and SchoolRosterSlot. Or maybe you could add start_date and end_date to SchoolRosterSlot to denote when a player starts and leaves that slot?

Upvotes: 1

Related Questions