leonel
leonel

Reputation: 10224

Rails 3 ActiveAdmin. How to setup has_many and belongs_to with a different foreign key?

So I have a shipment that can have up to three shipper companies. So I have this...

shipment belongs_to shipper
shipper has_many shipments

But I added two more columns to the shipments table: shipper_id_2 and shipper_id_3. How can I setup the association and also have ActiveAdmin realize it?

Upvotes: 2

Views: 626

Answers (1)

mbillard
mbillard

Reputation: 38882

I would recommend using a class in-between those two to assign the shipments to the shippers.

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

You can enforce the limit of 3 shippers with validators.

Upvotes: 1

Related Questions