我正在尝试通过命名外键将原始和目标端口城市(存储在Port模型中)与Job模型中的记录相关联。我已经搜索过这种关系的许多例子,但我仍然无法通过rails控制台插入新的Port记录。在尝试创建端口记录之前创建作业。以下是关联定义:
class Job < ApplicationRecord
has_many :destinations, :class_name => 'Port', :foreign_key => 'destination_id'
has_many :origins, :class_name => 'Port', :foreign_key => 'origin_id'
end
class Port < ApplicationRecord
belongs_to :origin, :class_name => "Job"
belongs_to :destination, :class_name => "Job"
end
架构(已编辑):
create_table "jobs", force: :cascade do |t|
t.integer "origin_id"
t.integer "destination_id"
t.index ["destination_id"], name: "index_jobs_on_destination_id"
t.index ["origin_id"], name: "index_jobs_on_origin_id"
end
create_table "ports", force: :cascade do |t|
t.string "name"
t.float "lat"
t.float "lon"
end
我正在寻找在设置这些模型的迁移中使用的外键的确切语法。有什么想法吗?