让质量属性分配只有在创造 [英] Make attributes mass assignable only during creation

查看:171
本文介绍了让质量属性分配只有在创造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时有可能有创建一个模型对象中的属性,这只是大规模分配?

Is it possible to have an attribute that is only mass-assignable during 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).

推荐答案

这是什么 attr_readonly 作用:

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'

这篇关于让质量属性分配只有在创造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆