【发布时间】:2017-11-03 12:37:02
【问题描述】:
我有一个Consultation 模型,它有一个post_consultant 和一个consultant。 post_consultant 和 consultant 都是对 Employee 模型的引用。所以你可以说:
型号
Class Consultation < ActiveRecord::Base
has_one :employee # for consultant
has_one :employee # for post_consultant
end
迁移
create_table "consultations", force: :cascade do |t|
t.boolean "showed_up"
t.boolean "signed_up"
t.integer "client_id"
t.integer "consultant_id"
t.integer "post_consultant_id"
end
我该怎么写?
正确模型:
class Consultation < ActiveRecord::Base
belongs_to :consultant, class_name: "Employee", foreign_key: "consultant_id"
belongs_to :post_consultant, class_name: "Employee", foreign_key: "post_consultant_id"
end
【问题讨论】:
-
post_consultant和consultant是模特吗?
-
post_consultant 和 advisor 哪个外键?
-
只有员工是模特
标签: ruby-on-rails activerecord associations