通过多个字段与一种类型的项目的关系 - SQL Rails Active Record

时间:2014-08-07 08:14:26

标签: ruby-on-rails activerecord

我想创建line_items和货件之间的关系。

其中订单项包含inbound_shipment_id和outbound_shipment_id,其中两者都引用相同的货件模型和表格

inbound_shipment_id foreign key - shipment table -- id
outbound_shipment_id foreign key - shipment table -- id

1 个答案:

答案 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