lundi 10 août 2015

Can a model belong to either one of two other models

I will try to explain this as best as I can.

I have 3 models.

DistributionEvent PrimaryAssignment SecondaryAssignment

I need the DistributionEvent to belong_to either a PrimaryAssignment or a SecondaryAssignment and show on the same column in the database.

So something like this

class DistributionEvent < ActiveRecord::Base
 belongs_to :primaryassignment
 belongs_to :secondaryassignment
end

class PrimaryAssignment < ActiveRecord::Base
 has_many :distribution_event
end

class SecondaryAssignment < ActiveRecord::Base
 has_many :distribution_event
end

But the DistributionEvent should only have one column (assignment or something) in the table that points to either primaryassignments or secondaryassignments.

I hope that make sense.

Anyone have any clue on how to achieve this efficiently?

Thanks!

Aucun commentaire:

Enregistrer un commentaire