我有以下型号:使用has_many创建记录:通过?
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
这是犯错误的W¯¯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模型找到并分配它.... – AnApprentice