2012-01-09 2 views
2

그래서 최대 3 개의 배송 업체를 보유 할 수있는 발송물이 있습니다. 그래서 ...레일즈 3 ActiveAdmin. 다른 외래 키로 has_many 및 belongs_to를 설정하는 방법은 무엇입니까?

shipment belongs_to shipper 
shipper has_many shipments 

을이이하지만 출하량 테이블에 두 개 이상의 열 추가 : shipper_id_2 및 shipper_id_3을. 연결을 설정하고 ActiveAdmin이이를 인식하게하려면 어떻게해야합니까?

+0

아래에서 GoodEnough가 제안하는 것은 아마도 올바른 방법 일 것입니다. 테이블에 다른 shipper_id를 유지하려는 경우 foreign_key 속성을 사용하여 열 이름 (예 : foriegn_key => '사용자의 연결에 추가 된 shipper_id_2)으로 설정하여 일치에 사용되는 외래 키를 설정할 수 있습니다. . –

답변

1

발송인에게 발송물을 지정하려면 해당 두 클래스 사이에 클래스를 사용하는 것이 좋습니다.

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 개 화주의 제한을 적용 할 수 있습니다.