Skip to content

Commit

Permalink
Improve ActiveRecord::Core#readonly! docs
Browse files Browse the repository at this point in the history
  • Loading branch information
fxn committed Dec 5, 2024
1 parent 17a5ae1 commit 0c37073
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 0c37073

Please sign in to comment.