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?
.
Now, when we try to update or delete the model we get an error.
Both options raise the ActiveRecord::ReadOnlyRecord
error.
However when we try to touch the model we get no error.
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
Now readonly models are no longer updated when update_columns
or touch
is called.