Skip to content

Commit ba2603a

Browse files
authored
Merge pull request rails#50682 from p8/activerecord/readonly-examples
Add code examples to `readonly` documentation [skip ci]
2 parents 21648f4 + 64b2417 commit ba2603a

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

activerecord/lib/active_record/core.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,10 @@ def strict_loading_n_plus_one_only?
697697
end
698698

699699
# Marks this record as read only.
700+
#
701+
# customer = Customer.first
702+
# customer.readonly!
703+
# customer.save # Raises an ActiveRecord::ReadOnlyRecord
700704
def readonly!
701705
@readonly = true
702706
end

guides/source/active_record_querying.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,7 @@ Active Record provides the [`readonly`][] method on a relation to explicitly dis
13051305
```ruby
13061306
customer = Customer.readonly.first
13071307
customer.visits += 1
1308-
customer.save
1308+
customer.save # Raises an ActiveRecord::ReadOnlyRecord
13091309
```
13101310

13111311
As `customer` is explicitly set to be a readonly object, the above code will raise an `ActiveRecord::ReadOnlyRecord` exception when calling `customer.save` with an updated value of _visits_.

0 commit comments

Comments
 (0)