From 64b24171c46f4e2c64ed1e6c2ace54a1ccbdbb56 Mon Sep 17 00:00:00 2001 From: Petrik Date: Tue, 9 Jan 2024 20:54:27 +0100 Subject: [PATCH] Add code examples to `readonly` documentation --- activerecord/lib/active_record/core.rb | 4 ++++ guides/source/active_record_querying.md | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index 0d6d23e67f9..5393ed84fe6 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -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 diff --git a/guides/source/active_record_querying.md b/guides/source/active_record_querying.md index 3a1ddce891a..81779c41125 100644 --- a/guides/source/active_record_querying.md +++ b/guides/source/active_record_querying.md @@ -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_.