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.
This can be achieved by adding a
readonly? method to the class which returns
Now, when we try to update or delete the model we get an error.
Both options raise the
However when we try to touch the model we get no error.
touch call doesn’t do much damage,
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.
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
touch models within ActiveRecord fixed this.
Now readonly models are no longer updated when
touch is called.