Ruby on rails Rails3ActiveAdmin。如何使用不同的外键设置“有多个”和“属于”?

Ruby on rails Rails3ActiveAdmin。如何使用不同的外键设置“有多个”和“属于”?,ruby-on-rails,ruby,associations,activeadmin,Ruby On Rails,Ruby,Associations,Activeadmin,所以我有一批货,最多可以有三家托运公司。所以我有这个 shipment belongs_to shipper shipper has_many shipments 但我在发货表中又添加了两列:shipper_id_2和shipper_id_3。如何设置关联并让ActiveAdmin实现它?我建议使用介于这两者之间的类将装运分配给发货人 class ShippingAssignments belongs_to :shipment belongs_to :shipper end clas

所以我有一批货,最多可以有三家托运公司。所以我有这个

shipment belongs_to shipper
shipper has_many shipments

但我在发货表中又添加了两列:shipper_id_2和shipper_id_3。如何设置关联并让ActiveAdmin实现它?

我建议使用介于这两者之间的类将装运分配给发货人

class ShippingAssignments
  belongs_to :shipment
  belongs_to :shipper
end

class Shipment
  has_many :shipping_assignments
  has_many :shippers, :through => :shipping_assignments
end

class Shipper
  has_many :shipping_assignments
  has_many :shipments, :through => :shipping_assignments
end

您可以使用验证器强制执行3个托运人的限制。

GoodEnough下面提出的建议可能是正确的前进方向。如果要继续在表中保留其他发货人id,可以通过使用:foreign_key属性并将其设置为列名(例如:foreign_key=>“shipper_id_2附加到关联中)来设置用于匹配的外键。