Rails has_and_belongs_to_many创建/插入不起作用

时间:2012-06-28 03:02:40

标签: ruby-on-rails ruby-on-rails-3 activerecord

我有以下型号 -

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_idnull ??

1 个答案:

答案 0 :(得分:1)

我认为你想在这里使用员工类的实例,而不是类本身。

像:

employee = Employee.first
employee.surveys.create(name: "New Survey")