开发者

Make attributes mass assignable only during creation

开发者 https://www.devze.com 2023-04-03 20:54 出处:网络
Is it possible to have an attribute that is only mass-assignable dur开发者_开发知识库ing the creation of a model object?

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

精彩评论

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