File tree Expand file tree Collapse file tree 2 files changed +7
-5
lines changed Expand file tree Collapse file tree 2 files changed +7
-5
lines changed Original file line number Diff line number Diff line change 17
17
config.active_record.raise_on_assign_to_attr_readonly = true
18
18
` ` `
19
19
20
- and will be enabled by default with ` load_defaults 7.1` .
20
+ and will be enabled by default with ` config. load_defaults 7.1` .
21
21
22
22
* Alex Ghiculescu * , * Hartley McGuire *
23
23
Original file line number Diff line number Diff line change @@ -12,10 +12,11 @@ module ReadonlyAttributes
12
12
end
13
13
14
14
module ClassMethods
15
- # Attributes listed as readonly will be used to create a new record but update operations will
16
- # ignore these fields .
15
+ # Attributes listed as readonly will be used to create a new record.
16
+ # Assigning a new value to a readonly attribute on a persisted record raises an error .
17
17
#
18
- # You can assign a new value to a readonly attribute, but it will be ignored when the record is updated.
18
+ # By setting `config.active_record.raise_on_assign_to_attr_readonly` to `false`, it will
19
+ # not raise. The value will change in memory, but will not be persisted on `save`.
19
20
#
20
21
# ==== Examples
21
22
#
@@ -24,7 +25,8 @@ module ClassMethods
24
25
# end
25
26
#
26
27
# post = Post.create!(title: "Introducing Ruby on Rails!")
27
- # post.update(title: "a different title") # change to title will be ignored
28
+ # post.title = "a different title" # raises ActiveRecord::ReadonlyAttributeError
29
+ # post.update(title: "a different title") # raises ActiveRecord::ReadonlyAttributeError
28
30
def attr_readonly ( *attributes )
29
31
new_attributes = attributes . map ( &:to_s ) . reject { |a | _attr_readonly . include? ( a ) }
30
32
You can’t perform that action at this time.
0 commit comments