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

Add docs for attr_readonly

From https://github.com/rails/rails/pull/42705#issuecomment-878415276 - explicitly call out that assigning attributes works, but is ignored by saves.

Co-authored-by: Petrik de Heus <petrik@deheus.net>
This commit is contained in:
Alex Ghiculescu 2021-07-12 11:40:41 -05:00
parent e06706201a
commit 647f4bd17f

View file

@ -11,6 +11,17 @@ module ActiveRecord
module ClassMethods module ClassMethods
# Attributes listed as readonly will be used to create a new record but update operations will # Attributes listed as readonly will be used to create a new record but update operations will
# ignore these fields. # 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) def attr_readonly(*attributes)
self._attr_readonly = Set.new(attributes.map(&:to_s)) + (_attr_readonly || []) self._attr_readonly = Set.new(attributes.map(&:to_s)) + (_attr_readonly || [])
end end