0

I've been coming back to a problem I haven't solved on my own. I've got a shipments table with two columns for addresses, namely to_address_id and from_address_id.

After looking at a related question (Same Model for Two belongs_to Associations migration), I am still confused. Here is my work so far. I haven't run the migration. I'm trying to resolve this in my mind before getting worked up about it and waste an hour or two. I really thank you for your help.

class AddAddressFromAndAddressToToAddress < ActiveRecord::Migration[6.0]
  def change
    add_reference :address, :address_from, null: false
    add_reference :address, :address_to, null: false
    add_foreign_key :address, :shipments, column: :to_address_id
    add_foreign_key :address, :shipments, column: :from_address_id
  end
en
listenlight
  • 654
  • 1
  • 12
  • 27

2 Answers2

1

If i understand correctly you want add origin and destiny address to shipping, correctly?

In this case:

If you want create a migration to create a relationship

class AddOriginAndDestinyAddressToShipment < ActiveRecord::Migration[6.0]
  def change
    add_reference :shipments, :from_address, foreign_key: { to_table: :addresses }
    add_reference :shipments, :to_address, foreign_key: { to_table: :addresses }
  end
end

If you want create a model with a relationship

class CreateShipments < ActiveRecord::Migration[6.0]
  def change
    create_table :shipments do |t|
      t.datetime :deadline
      t.references :from_address, null: false, foreign_key: { to_table: :addresses }
      t.references :to_address, null: false, foreign_key: { to_table: :addresses }
      t.timestamps
    end
  end
end

And model Shipment must be something like this

class Shipment < ApplicationRecord
  belongs_to :to_address, class_name: 'Address'
  belongs_to :from_address, class_name: 'Address'
end
thiaguerd
  • 807
  • 1
  • 7
  • 16
0

I did not need the migration, since the foreign keys were already in place. The one I tried didn't work, and the strong_migrations gem was complaining. I only had to wire it up right.

I wanted to show the way I've saved some confusion, so I can type shipment.to_address or shipment.address_to and get the same result. Thank you.

class Shipment < ApplicationRecord
  belongs_to :address_from, class_name: 'Address', foreign_key: :from_address_id
  belongs_to :from_address, class_name: 'Address', foreign_key: :from_address_id
  belongs_to :address_to,   class_name: 'Address', foreign_key: :to_address_id
  belongs_to :to_address,   class_name: 'Address', foreign_key: :to_address_id
end

class Address < ApplicationRecord
  has_many :shipments
end
listenlight
  • 654
  • 1
  • 12
  • 27