# Can't mass-assign protected attributes:

**URL:** <https://discuss.rubyonrails.org/t/cant-mass-assign-protected-attributes/64116>\
**Category:** rubyonrails-talk\
**Created:** [June 7, 2012, 10:08pm UTC](https://discuss.rubyonrails.org/t/cant-mass-assign-protected-attributes/64116 "2012-06-07T22:08:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [June 7, 2012, 10:08pm UTC](https://discuss.rubyonrails.org/t/cant-mass-assign-protected-attributes/64116/1 "2012-06-07T22:08:22Z")

</div>

Hi,

i added a column to my DB using rails g migration AddAttrToTablename attr:string rake db:migrate

I didnt change the model file. Should i?

Now when i create a new record to the table assigning values to existing attrs and newly added attribute i get this error "Can't mass-assign protected attributes: attr"

Any1 understand whats happening here?

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [June 8, 2012, 7:02am UTC](https://discuss.rubyonrails.org/t/cant-mass-assign-protected-attributes/64116/2 "2012-06-08T07:02:19Z")

</div>

> Hi,
> 
> i added a column to my DB using rails g migration AddAttrToTablename attr:string rake db:migrate
> 
> I didnt change the model file. Should i?
> 
> Now when i create a new record to the table assigning values to existing attrs and newly added attribute i get this error "Can't mass-assign protected attributes: attr"
> 
> Any1 understand whats happening here?

You probably need to add the new field to the attr\_accessible list in the model.rb file so you can do such things as update\_attributes( params[...]) or new( params[...] )

Colin

---

<div class="post-metadata">

**Author:** ![MUMU](https://avatars.discourse-cdn.com/v4/letter/m/977dab/32.png) [@MUMU](https://discuss.rubyonrails.org/u/MUMU)\
**Post date:** [June 8, 2012, 8:36am UTC](https://discuss.rubyonrails.org/t/cant-mass-assign-protected-attributes/64116/3 "2012-06-08T08:36:47Z")

</div>

Hi Colin, I think u r using rails 3.2.x where mass assignment is protected for saving the record into the db you need to write this piece of code in model

attr\_accessible : field/column name

_ **With Thanks & Regards, Santosh Mohanty** __Software Developer, Railsfactory(Ruby on Rails Solution)_\* \*☎ 9445808957

[www.railsfactory.org](http://www.railsfactory.org)

Contact me: ![Google Talk](https://global.discourse-cdn.com/rubyonrails/original/2X/9/9f36a77f15b2356381fe7b7cf9b3a00cb66a855c.png) [santosh@railsfactory.org](mailto:santosh@railsfactory.org) ![Skype](https://global.discourse-cdn.com/rubyonrails/original/2X/9/92a53df4cb664feb6bb4d3c46840b8ea68aa1617.png) santosh.railsfactory

[Want a signature like mine?](http://r1.wisestamp.com/r/landing?promo=22&dest=http%3A%2F%2Fwww.wisestamp.com%2Femail-install%3Futm_source%3Dextension%26utm_medium%3Demail%26utm_campaign%3Dpromo_22)[Click here.](http://r1.wisestamp.com/r/landing?promo=22&dest=http%3A%2F%2Fwww.wisestamp.com%2Femail-install%3Futm_source%3Dextension%26utm_medium%3Demail%26utm_campaign%3Dpromo_22)

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [June 8, 2012, 8:51am UTC](https://discuss.rubyonrails.org/t/cant-mass-assign-protected-attributes/64116/4 "2012-06-08T08:51:52Z")

</div>

> Hi Colin,

It is best not to top post as doing this makes it more difficult to follow the thread, and also makes it easier to reply to the wrong person. cyber c was the original poster. Best to insert your reply at appropriate points in the previous message.

> I think u r using rails 3.2.x where mass assignment is protected for saving the record into the db
> 
> you need to write this piece of code in model
> 
> attr\_accessible : field/column name

Also if you had posted this after my comment which said

"You probably need to add the new field to the attr\_accessible list in

the model.rb file so you can do such things as update\_attributes(

params[…]) or new( params[…] )"

then you would have noticed that I had already answered the question.

> _ **With Thanks & Regards, Santosh Mohanty** __Software Developer, Railsfactory(Ruby on Rails Solution)_\* \*☎ 9445808957

> [www.railsfactory.org](http://www.railsfactory.org)

> Contact me: [santosh@railsfactory.org](mailto:santosh@railsfactory.org) santosh.railsfactory

> [Want a signature like mine?](http://r1.wisestamp.com/r/landing?promo=22&dest=http%3A%2F%2Fwww.wisestamp.com%2Femail-install%3Futm_source%3Dextension%26utm_medium%3Demail%26utm_campaign%3Dpromo_22)[Click here.](http://r1.wisestamp.com/r/landing?promo=22&dest=http%3A%2F%2Fwww.wisestamp.com%2Femail-install%3Futm_source%3Dextension%26utm_medium%3Demail%26utm_campaign%3Dpromo_22)

> 

Why would I want a signature like yours?

Colin

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [June 9, 2012, 11:54am UTC](https://discuss.rubyonrails.org/t/cant-mass-assign-protected-attributes/64116/5 "2012-06-09T11:54:36Z")

</div>

> Indeed, the right way would have been to use the generator to add a field to your model. This would have update your model AND created a DB migration. In your case, you changed the DB, but not the Ruby object that corresponds to it. So indeed the new field does not exist in your object, thus the error.

What generator command would have added attr\_accessible for the new field in the model.rb?

Colin

---

<div class="post-metadata">

**Author:** ![Gregory\_Bataille](https://avatars.discourse-cdn.com/v4/letter/g/c2a13f/32.png) [@Gregory\_Bataille](https://discuss.rubyonrails.org/u/Gregory_Bataille)\
**Post date:** [June 9, 2012, 5:50pm UTC](https://discuss.rubyonrails.org/t/cant-mass-assign-protected-attributes/64116/6 "2012-06-09T17:50:49Z")

</div>

hum, you’re right, I mixed things up in my head. I indeed don’t know of any ☹
