ruby on rails 3 - Designing the relationship between models where the child model attributes vary based on the "type" of its parent -



ruby on rails 3 - Designing the relationship between models where the child model attributes vary based on the "type" of its parent -

how should go designing relationship between models when kid model's attributes vary based on parent type belongs to?

for example: player has_many :projections fields on instance of projection vary depending on "type" of player belongs to.

class player < activerecord::base attr_accessible :name, :position, :team has_many :projections validates_uniqueness_of :name, :scope => [:position, :team] end class projection < activerecord::base attr_accessible :ppr, :salary, :score, :week, :player_id belongs_to :player validates_uniqueness_of :ppr, :scope => [:week, :player_id] end

so, want modify , add together attributes/fields projection fields different depending on "position" attribute of player model

examples of new fields added are:

:passing, :completions, :receptions, :targets

a projection player position of "qb" this:

projection.create(passing: 300, completions: 30, week: 8, player_id: 1)

while projection player position "wr" this:

projection.create(receptions: 5, targets: 9, week: 8, player_id: 2)

player id 1 of position type "qb" while player id 2 of position type "wr"

they both still "projections" except hold different types of info based on position of player. current schema not business relationship players position. currently, share same attributes. right way of modifying existing schema business relationship these new info points? i'm worried might break existing code when modifying add together new feature.

it hard give definite reply without knowing more application , utilize cases.

but describe i'd advise add together 4 new columns projections table (namely passing, completions, receptions , targets). fill , read them needed depending on player's position, while values can remain null.

if need custom behaviour (like validations) different player's projections, introducing types of projections might help (i.e. single-table inheritance). remembering player's position on projection in 1 form or might thought anyway in case player changes position @ point. maybe don't need because can display columns contain values given projection.

hope helps!

ruby-on-rails-3 inheritance activerecord database-design

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -