如何为具有相同关系的两个外键的模型编写关联?
How to write associations for a model with two foreign keys for the same relationship?
我一直在回到一个我自己没有解决的问题。我有一个 shipments
table,其中有两列用于 addresses
,即 to_address_id
和 from_address_id
。
看了一个相关问题(),还是一头雾水。到目前为止,这是我的工作。我没有 运行 迁移。我试图在脑海中解决这个问题,然后再为此努力并浪费一两个小时。真的很感谢你的帮助。
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
如果我理解正确的话,你想在运输中添加始发地和目的地地址,对吗?
在这种情况下:
如果要创建迁移以创建关系
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
如果你想创建一个有关系的模型
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
模型 Shipment 必须是这样的
class Shipment < ApplicationRecord
belongs_to :to_address, class_name: 'Address'
belongs_to :from_address, class_name: 'Address'
end
我不需要迁移,因为外键已经就位。我试过的那个没有用,strong_migrations
gem 在抱怨。我只需要正确连接即可。
我想展示我如何避免一些混乱,所以我可以键入 shipment.to_address
或 shipment.address_to
并获得相同的结果。谢谢。
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
我一直在回到一个我自己没有解决的问题。我有一个 shipments
table,其中有两列用于 addresses
,即 to_address_id
和 from_address_id
。
看了一个相关问题(
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
如果我理解正确的话,你想在运输中添加始发地和目的地地址,对吗?
在这种情况下:
如果要创建迁移以创建关系
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
如果你想创建一个有关系的模型
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
模型 Shipment 必须是这样的
class Shipment < ApplicationRecord
belongs_to :to_address, class_name: 'Address'
belongs_to :from_address, class_name: 'Address'
end
我不需要迁移,因为外键已经就位。我试过的那个没有用,strong_migrations
gem 在抱怨。我只需要正确连接即可。
我想展示我如何避免一些混乱,所以我可以键入 shipment.to_address
或 shipment.address_to
并获得相同的结果。谢谢。
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