我正在为我的一张表创建种子数据,并且每当我运行rake db:seed时,都会给我错误:
无法大规模分配受保护的属性:严重性
我的两个模型看起来像
class Status < ActiveRecord::Base
belongs_to :severity
attr_accessible :description, :image, :name, :slug, :severity_id
end
和
class Severity < ActiveRecord::Base
attr_accessible :name, :val, :severity_id
end
我尝试使用的数据是
statuses = Status.create(
[
{
"name"=> 'Normal',
"slug"=> 'normal',
"description"=> 'The service is up or was up during this entire period',
"severity"=> 1,
"image"=> 'tick-circle'
}
]
)
为什么会这样?
您需要在attr_accesible行的Severity模型中添加:severity。 Rails尝试使用该名称(我认为您在数据库中具有的名称)分配属性。
attr_accessible :severity
您的种子说severity
,但访问者说severity_id
。那是哪一个?