开发者

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'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜