Skip to content

Commit 59566ca

Browse files
authored
Merge pull request rails#53836 from rails/fxn/readonly-docs
Improve ActiveRecord::Core#readonly! docs
2 parents 1ca0f27 + 0c37073 commit 59566ca

File tree

1 file changed

+20
-2
lines changed
  • activerecord/lib/active_record

1 file changed

+20
-2
lines changed

activerecord/lib/active_record/core.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,11 +727,29 @@ def strict_loading_all?
727727
@strict_loading_mode == :all
728728
end
729729

730-
# Marks this record as read only.
730+
# Prevents records from being written to the database:
731+
#
732+
# customer = Customer.new
733+
# customer.readonly!
734+
# customer.save # raises ActiveRecord::ReadOnlyRecord
735+
#
736+
# customer = Customer.first
737+
# customer.readonly!
738+
# customer.update(name: 'New Name') # raises ActiveRecord::ReadOnlyRecord
739+
#
740+
# Read-only records cannot be deleted from the database either:
731741
#
732742
# customer = Customer.first
733743
# customer.readonly!
734-
# customer.save # Raises an ActiveRecord::ReadOnlyRecord
744+
# customer.destroy # raises ActiveRecord::ReadOnlyRecord
745+
#
746+
# Please, note that the objects themselves are still mutable in memory:
747+
#
748+
# customer = Customer.new
749+
# customer.readonly!
750+
# customer.name = 'New Name' # OK
751+
#
752+
# but you won't be able to persist the changes.
735753
def readonly!
736754
@readonly = true
737755
end

0 commit comments

Comments
 (0)