无法批量分配受保护的属性

xyz*_*ace 2 ruby ruby-on-rails mass-assignment

我已经阅读了关于这个问题的一些SO文章,但它们似乎都没有起作用.我正在为我的一个表创建种子数据,每当我运行rake db:seed时它会给我错误:

Can't mass-assign protected attributes: severity
Run Code Online (Sandbox Code Playgroud)

我的两个模特看起来像

class Status < ActiveRecord::Base
  belongs_to :severity
  attr_accessible :description, :image, :name, :slug, :severity_id
end
Run Code Online (Sandbox Code Playgroud)

class Severity < ActiveRecord::Base
  attr_accessible :name, :val, :severity_id
end
Run Code Online (Sandbox Code Playgroud)

我试图种下的数据是

statuses = Status.create(
  [
    {
      "name"=> 'Normal', 
      "slug"=> 'normal', 
      "description"=> 'The service is up or was up during this entire period', 
      "severity"=> 1,
      "image"=> 'tick-circle'
    }
  ]
)
Run Code Online (Sandbox Code Playgroud)

我很难理解为什么会这样.有什么建议吗?

提前致谢

JEM*_*dux 5

您需要在attr_accesible行上的严重性模型中添加:severity.Rails正在尝试按照我假设您在数据库中拥有的名称来分配属性.