Make attributes mass assignable only during creation
Is it possible to have an attribute that is only mass-assignable dur开发者_开发知识库ing the creation of a model object?
For example, the username
attribute should be mass-assignable when creating the object, but not after that (it should be read-only).
This is what attr_readonly
does:
class User < ActiveRecord::Base
attr_readonly :username
end
u = User.create(:username => 'dude')
u.username # => 'dude'
u.update_attributes(:username => 'dudette')
u.reload.username # => 'dude'
精彩评论