

ActiveRecord methods touch and update_columns no longer work for readonly models
source link: https://blog.saeloun.com/2022/08/24/readonly-touch-update-attr-fix
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

A Rails model has several ActiveRecord methods that make interacting with the database very simple. Models can be created, updated and destroyed very easily.
However, in some situations, one might want to mark a model as “readonly”. Meaning that only read operations are allowed — no updates or deletions. A situation like this may occur in the middle of a big refactoring. A model has been retired but we may not be ready to wipe it out. We might still need to read it.
Before
This can be achieved by adding a readonly?
method to the class which returns true?
.
class Rocket
...
def readonly?
true
end
end
Now, when we try to update or delete the model we get an error.
irb(main):001:0> rocket.update name: "A1"
TRANSACTION (5.4ms) BEGIN
User Load (6.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
TRANSACTION (1.8ms) ROLLBACK
Traceback (most recent call last):
/Users/swaathi/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/bundler/gems/rails-08af60e01dcf/activerecord/lib/active_record/persistence.rb:1147:in `_raise_readonly_record_error': Rocket is marked as readonly (ActiveRecord::ReadOnlyRecord)
irb(main):002:0> rocket.destroy
Traceback (most recent call last):
/Users/swaathi/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/bundler/gems/rails-08af60e01dcf/activerecord/lib/active_record/persistence.rb:1147:in `_raise_readonly_record_error': Rocket is marked as readonly (ActiveRecord::ReadOnlyRecord)
Both options raise the ActiveRecord::ReadOnlyRecord
error.
However when we try to touch the model we get no error.
irb(main):002:0> rocket.touch
TRANSACTION (0.5ms) BEGIN
Rocket Update (14.2ms) UPDATE `rockets` SET `rockets`.`updated_at` = '2022-08-14 06:40:24.291104' WHERE `rockets`.`id` = 1
TRANSACTION (5.6ms) COMMIT
=> true
While a touch
call doesn’t do much damage,
the update_columns
call does.
Unfortunately, marking a model as readonly
does not prevent us from updating the model in all possible ways.
This corrupts the data with unwarranted updates.
After
People were too quick to point out this discrepancy, however, the Rails team was even quicker to issue a patch!
A simple update to the update_columns
and touch
models within ActiveRecord fixed this.
activerecord/lib/active_record/persistence.rb
...
module ActiveRecord
module Persistence
extend ActiveSupport::Concern
module ClassMethods
def update_columns
...
_raise_readonly_record_error if readonly?
...
end
end
end
end
Now readonly models are no longer updated when update_columns
or touch
is called.
Recommend
-
36
During our trip aroundmanaged pointers andstructs, we get the last topic to discuss – readonly semantics . Thus, we touch today topics like readonly structs and readonly paremeters. Readon...
-
6
Quick Tip – Using to_s as a label and simplified link_to calls to your ActiveRecord models One of the things you’ll find in every rails application is links like this one: <%= link_to u...
-
8
欢迎转载,请支持原创,保留原文链接:blog.ilibrary.me Monkey patch 这篇日志起源于rail...
-
8
Why classes eventually reach 50 columns and hundreds of methods There are dozens of small or bigger problems that we can have in our code. Like diseases, they affect our applications and make them harder to maintain,...
-
10
ActiveRecord: Skipping callbacks like after_save or after_update Posted: 2009-06-07 - Last updated: 2019-06-05 Tagged...
-
9
Rails 6.1 adds ActiveRecord methods `#sole` and `#find_sole_by` Mar 16, 2021 , by Swaathi Kakarla 1 minute read Inspired by Django’s
-
10
Rails is a large framework with a lot of handy tools built-in for specific situations. In this series, we're taking a look at some of the lesser-known tools hidden in Rails' large codebase. In this article in the serie...
-
5
About this Episode Fellow thoughtboter Sarah Lima joins Joël to discuss an issue Sarah had when she was doing a code review recently: making HTTP requests in an ActiveRecord model. Her con...
-
4
Performance Implications Of Using Calculated Columns In Composite Models On Power BI Semantic Models I don’t have anything against the use of calculated columns in Power BI semantic models in general b...
-
6
Rails 7.1 Introduces Option To Disable All Methods That ActiveRecord.enum Generates Feb 5, 2024 • ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK