Skip to content

Commit 709ec24

Browse files
authored
Merge pull request rails#46523 from ghiculescu/patch-10 [ci-skip]
Fix docs for Readonly Attributes changes
2 parents eb303ad + 7b09475 commit 709ec24

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

activerecord/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
config.active_record.raise_on_assign_to_attr_readonly = true
1818
```
1919

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`.
2121

2222
*Alex Ghiculescu*, *Hartley McGuire*
2323

activerecord/lib/active_record/readonly_attributes.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ module ReadonlyAttributes
1212
end
1313

1414
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.
1717
#
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`.
1920
#
2021
# ==== Examples
2122
#
@@ -24,7 +25,8 @@ module ClassMethods
2425
# end
2526
#
2627
# 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
2830
def attr_readonly(*attributes)
2931
new_attributes = attributes.map(&:to_s).reject { |a| _attr_readonly.include?(a) }
3032

0 commit comments

Comments
 (0)