【发布时间】:2017-08-07 14:35:14
【问题描述】:
我有以下型号:
class Department < ApplicationRecord
has_many :department_job_titles
has_many :job_titles, through: :department_job_titles
end
class JobTitle < ApplicationRecord
has_and_belongs_to_many :departments
end
class DepartmentJobTitle < ApplicationRecord
belongs_to :department
belongs_to :job_title
validates :department_id, uniqueness: { scope: :job_title_id }
end
PG::UndefinedColumn: ERROR: column department_job_titles.title does not exist
LINE 1: ... "department_job_titles"."department_id" = $1 AND "departmen...这是错误的
Department.first.department_job_titles.find_or_create_by(title: title)
DepartmentJobTitle 具有以下字段:id, department_id, job_title_id
我在这里做错了什么?
【问题讨论】:
-
您是要添加一个新的
JobTitle,还是为现有的JobTitle添加一个新的DepartmentJobTitle,还是同时添加两者? -
我已经创建了部门...我是。试图用
Department.first来表示它...我现在正在尝试将 JobTitle 分配给部门,我需要创建 JobTitle 或通过 DepartmentJobTitle 模型查找并分配它...。
标签: ruby-on-rails activerecord ruby-on-rails-5 activemodel