Merge pull request #50682 from p8/activerecord/readonly-examples

Add code examples to `readonly` documentation [skip ci]
This commit is contained in:
fatkodima 2024-01-09 22:40:54 +02:00 committed by GitHub
commit ba2603ac0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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_.