Skip to content

Commit

Permalink
Merge pull request rails#53836 from rails/fxn/readonly-docs
Browse files Browse the repository at this point in the history
Improve ActiveRecord::Core#readonly! docs
  • Loading branch information
fxn authored Dec 5, 2024
2 parents 1ca0f27 + 0c37073 commit 59566ca
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions activerecord/lib/active_record/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -727,11 +727,29 @@ def strict_loading_all?
@strict_loading_mode == :all
end

# Marks this record as read only.
# Prevents records from being written to the database:
#
# customer = Customer.new
# customer.readonly!
# customer.save # raises ActiveRecord::ReadOnlyRecord
#
# customer = Customer.first
# customer.readonly!
# customer.update(name: 'New Name') # raises ActiveRecord::ReadOnlyRecord
#
# Read-only records cannot be deleted from the database either:
#
# customer = Customer.first
# customer.readonly!
# customer.save # Raises an ActiveRecord::ReadOnlyRecord
# customer.destroy # raises ActiveRecord::ReadOnlyRecord
#
# Please, note that the objects themselves are still mutable in memory:
#
# customer = Customer.new
# customer.readonly!
# customer.name = 'New Name' # OK
#
# but you won't be able to persist the changes.
def readonly!
@readonly = true
end
Expand Down

0 comments on commit 59566ca

Please sign in to comment.