It appears that update_attributes doesn’t allow me to change the id (so it IS protected), but why isn’t rails throwing the same error it does for other protected attributes?
> rails new mass_assignment_test
> cd mass_assignment_test
> rails g model User name:string
> rake db:migrate
> rails console
>> u = User.create(:name => "ben")
>> u.update_attributes(:id => 5)
=> true
>> u.id
=> 1
>> u.update_attributes(:created_at => Time.now)
ActiveModel::MassAssignmentSecurity:Error
This is the model that rails generates (app/models/user.rb):
class User < ActiveRecord::Base
attr_accessible :name
end
The id of a record is protected by Rails itself:
You can bypass this with:
Also, please please do not do this. 🙂 It will make your code hard to maintain…