mirror of https://github.com/rails/rails
Merge pull request #50682 from p8/activerecord/readonly-examples
Add code examples to `readonly` documentation [skip ci]
This commit is contained in:
commit
ba2603ac0d
|
@ -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
|
||||
|
|
|
@ -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_.
|
||||
|
|
Loading…
Reference in New Issue