我有以下型号 -
class Employee < ActiveRecord::Base
has_and_belongs_to_many :surveys, join_table: 'employee_surveys'
end
class Survey < ActiveRecord::Base
has_and_belongs_to_many :employees, join_table: 'employee_surveys'
end
当我执行Employee.first.surveys.create(name: "New Survey")
时会产生错误,抱怨employee_id
为null
??
答案 0 :(得分:1)
我认为你想在这里使用员工类的实例,而不是类本身。
像:
employee = Employee.first
employee.surveys.create(name: "New Survey")