Add code examples to `readonly` documentation

This commit is contained in:
Petrik 2024-01-09 20:54:27 +01:00
parent 7edf988d6c
commit 64b24171c4
2 changed files with 5 additions and 1 deletions

View File

@ -697,6 +697,10 @@ module ActiveRecord
end
# Marks this record as read only.
#
# customer = Customer.first
# customer.readonly!
# customer.save # Raises an ActiveRecord::ReadOnlyRecord
def readonly!
@readonly = true
end

View File

@ -1305,7 +1305,7 @@ Active Record provides the [`readonly`][] method on a relation to explicitly dis
```ruby
customer = Customer.readonly.first
customer.visits += 1
customer.save
customer.save # Raises an ActiveRecord::ReadOnlyRecord
```
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_.