10

I get this error "WARNING: Can't mass-assign protected attributes: races_attributes" , when following this http://railscasts.com/episodes/196-nested-model-form-part-1 on rails 3.

Where Races are a component of Events. This is my models/race.rb:

class Race < ActiveRecord::Base
belongs_to :event

attr_accessible :name, :unit
end

This is my models/event.rb:

class Event < ActiveRecord::Base
has_many :races, :dependent => :destroy

accepts_nested_attributes_for :races

attr_accessible :name, :date, :description, :location_name, :address_one, :address_two, :city, :state, :zip, :active, :races_attributes
end

Any Ideas?

Hosemeyer
  • 1,264
  • 4
  • 17
  • 29

2 Answers2

20

Shorter than using attr_accessible, safer than using whitelist_attributes: attr_protected

Just indicate the protected attributes, and Rails will infer that all others can be mass-assigned:

class MyClass < ActiveRecord::Base
  attr_protected :id
end

(I always have way more attributes that I want mass-assigned than the ones I want protected.)

JellicleCat
  • 28,480
  • 24
  • 109
  • 162
  • Just for reference, whitelist_attributes can be set in application.rb: `config.active_record.whitelist_attributes = true` in case you want to do a quick hack and not have to modify individual models. Just comment out the line. – qix Oct 15 '13 at 16:37
11

attr_accessible specifies that you can not mass-assign attributes, using save method, for example. So, if you change an attribute that is not defined with attr_accessible, you will get a warning because it will not actually be saved in the database.

shybovycha
  • 11,556
  • 6
  • 52
  • 82
Spyros
  • 46,820
  • 25
  • 86
  • 129