1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #42762 from ghiculescu/patch-5

Add docs for `attr_readonly`
This commit is contained in:
Ryuta Kamizono 2021-07-13 03:34:01 +09:00 committed by GitHub
commit 04121a5f30
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -11,6 +11,17 @@ module ActiveRecord
module ClassMethods
# Attributes listed as readonly will be used to create a new record but update operations will
# ignore these fields.
#
# You can assign a new value to a readonly attribute, but it will be ignored when the record is updated.
#
# ==== Examples
#
# class Post < ActiveRecord::Base
# attr_readonly :title
# end
#
# post = Post.create!(title: "Introducing Ruby on Rails!")
# post.update(title: "a different title") # change to title will be ignored
def attr_readonly(*attributes)
self._attr_readonly = Set.new(attributes.map(&:to_s)) + (_attr_readonly || [])
end