我有一个有很多飞行员的船模。飞行员可以是角色或派系。我可以使用has_many:through association来对此进行建模吗?
class Ship < ActiveRecord::Base
has_many :pilots, :through => :pilot_assignments, :polymorphic => true
has_many :pilot_assignments
end
class Character < ActiveRecord::Base
has_many :ships, :through => :pilot_assignments
has_many :pilot_assignments
end
class Faction < ActiveRecord::Base
has_many :ships, :through => :pilot_assignments
has_many :pilot_assignments
end
class PilotAssignment < ActiveRecord::Base
belongs_to :ship
belongs_to :pilot, :polymorphic => true
end
答案 0 :(得分:0)
class PilotAssignment < ActiveRecord::Base
belongs_to :ship
belongs_to :pilot, polymorphic: true
end
class Ship < ActiveRecord::Base
# i'm not sure should be there some additional options or not (like as: :pilot)
has_many :pilots, through: :pilot_assignments
has_many :pilot_assignments
end
class Character < ActiveRecord::Base
has_many :ships, through: :pilot_assignments
has_many :pilot_assignments, as: :pilot
end
class Faction < ActiveRecord::Base
has_many :ships, through: :pilot_assignments
has_many :pilot_assignments, as: :pilot
end
PilotAssignment
也应包含:pilot_type
,pilot_id