我想创建line_items和货件之间的关系。
其中订单项包含inbound_shipment_id和outbound_shipment_id,其中两者都引用相同的货件模型和表格
inbound_shipment_id foreign key - shipment table -- id
outbound_shipment_id foreign key - shipment table -- id
答案 0 :(得分:2)
以下行应正确设置您的关联
belongs_to :inbound_shipment, class_name: 'Shipment'
belongs_to :outbound_shipment, class_name: 'Shipment'
<强>更新强>
在Shipment
模型中,假设line_items与货件之间的关系是一对多,请使用以下关联
has_many :inbound_line_items, class_name: 'LineItem', foreign_key: :inbound_shipment_id
has_many :outbound_line_items, class_name: 'LineItem', foreign_key: :outbound_shipment_id