Rails中如何在单个表内创建两个关联Location表的外键?
实现单表关联两个同模型外键的操作步骤
1. 修改迁移文件配置
你之前的命令仅生成单个外键,是因为仅声明了一次location:references,且同表不能存在重名字段,你需要给两个指向Location表的外键设置不同别名,比如start_location和end_location。
首先执行生成命令:rails generate model Distance distance:integer start_location:references end_location:references
生成后打开db/migrate/下对应的迁移文件,修改t.references配置,指定外键指向locations表:
class CreateDistances < ActiveRecord::Migration[7.0] # 版本号以你实际生成的为准 def change create_table :distances do |t| t.integer :distance # 新增foreign_key配置指定关联表 t.references :start_location, null: false, foreign_key: { to_table: :locations } t.references :end_location, null: false, foreign_key: { to_table: :locations } t.timestamps end end end
修改完成后执行迁移:rails db:migrate
2. 配置模型关联
Distance 模型配置
需要指定两个关联的目标类名为Location:
class Distance < ApplicationRecord belongs_to :start_location, class_name: 'Location' belongs_to :end_location, class_name: 'Location' end
Location 模型配置(按需添加反向关联)
如果需要从地点侧查询关联的距离记录,添加反向关联配置:
class Location < ApplicationRecord # 作为起点的距离记录 has_many :origin_distances, class_name: 'Distance', foreign_key: 'start_location_id' # 作为终点的距离记录 has_many :destination_distances, class_name: 'Distance', foreign_key: 'end_location_id' end
3. 调用示例
完成配置后即可正常创建、查询记录:
# 创建距离记录 loc1 = Location.find(1) loc2 = Location.find(2) Distance.create(start_location: loc1, end_location: loc2, distance: 6) # 查询距离的起点、终点信息 distance = Distance.first puts distance.start_location.name # 输出对应起点名称 puts distance.end_location.name # 输出对应终点名称
内容的提问来源于stack exchange,提问作者Brad West
相关产品推荐
相关产品推荐

