2

I'm creating seed data for one of my tables and whenever I run rake db:seed it gives me the error:

Can't mass-assign protected attributes: severity

My two models look like

class Status < ActiveRecord::Base
  belongs_to :severity
  attr_accessible :description, :image, :name, :slug, :severity_id
end

and

class Severity < ActiveRecord::Base
  attr_accessible :name, :val, :severity_id
end

the data I'm trying to seed with is

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

Why does this happen?

Cœur
  • 37,241
  • 25
  • 195
  • 267
xyzjace
  • 432
  • 1
  • 5
  • 16
  • Related: [Can't mass assign protected attributes](https://stackoverflow.com/questions/9742842/cant-mass-assign-protected-attributes) – Cœur Feb 23 '20 at 04:48

3 Answers3

5

You need to add :severity to the Severity model on the attr_accesible line . Rails is trying to assign an attribute by that name which I assume you have in your database.

JEMaddux
  • 303
  • 3
  • 14
2
attr_accessible :severity

Section 6: Mass Assignment http://guides.rubyonrails.org/security.html

Kiattisak Anoochitarom
  • 2,157
  • 1
  • 20
  • 15
  • Thanks very much! Great article as well. Wasn't really sure what mass-assignment was doing for me. – xyzjace May 11 '13 at 02:43
1

Your seed says severity, but your accessor says severity_id. So which one is it?

Niels B.
  • 5,912
  • 3
  • 24
  • 44