2013-05-11 130 views
2

我已阅读了关于此问题的一些SO文章,但没有一篇似乎在工作。我为我的一个表创建种子数据,每当我耙运行分贝:种子它给我的错误:无法批量分配受保护的属性

Can't mass-assign protected attributes: severity 

我的两个车型看起来像

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' 
    } 
    ] 
) 

我正在努力理解为什么会发生这种情况。有什么建议吗?

在此先感谢

回答

5

您需要添加:严重程度对attr_accesible行的严重性模型。 Rails试图通过那个名字来分配一个属性,我假设你在你的数据库中有这个属性。

+1

谢谢,完全看到我现在出错的地方。 – xyzjace 2013-05-11 02:42:53

1

你的种子说:severity,但你的访问说:severity_id。那么是哪一个呢?

+0

我不敢相信我以前没有看到过。它总是小事。谢谢! – xyzjace 2013-05-11 02:42:32

相关问题