方法
实例公共方法
attr_readonly(*attributes) 链接
列为只读的Attributes
将用于创建新记录。为持久记录的只读属性分配新值会引发错误。
通过将config.active_record.raise_on_assign_to_attr_readonly
设置为false
,它不会引发。该值将在内存中更改,但不会在save
中持久化。
示例
class Post < ActiveRecord::Base
attr_readonly :title
end
post = Post.create!(title: "Introducing Ruby on Rails!")
post.title = "a different title" # raises ActiveRecord::ReadonlyAttributeError
post.update(title: "a different title") # raises ActiveRecord::ReadonlyAttributeError
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/readonly_attributes.rb, line 30 def attr_readonly(*attributes) self._attr_readonly |= attributes.map(&:to_s) if ActiveRecord.raise_on_assign_to_attr_readonly include(HasReadonlyAttributes) end end
readonly_attributes() 链接
返回已指定为只读的所有属性的数组。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/readonly_attributes.rb, line 39 def readonly_attributes _attr_readonly end