Repository navigation
Conversation
A virtual attribute is only loaded when its reader runs, so reading it through read_attribute returned nil. Overriding read_attribute rather than [] covers both, since [] reads through it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A virtual attribute is only populated once its reader has run, so
read_attributereturnsnilfor an encrypted attribute, while every other attribute-level reader returns the value:This is the same bug as #82 (
[], fixed in 95ad80e) and #43 (attributes, fixed in d8ed71f), one accessor over.Why override
read_attributerather than add to[]In Active Record,
[]isread_attribute:— activerecord/lib/active_record/attribute_methods.rb#L415-L417
So moving the warming down to
read_attributecovers both paths with one method —test_keyed_getterpasses unchanged — and anything else reading an attribute through it is covered as well. The write side needs nothing: it is already synced centrally in_create_record/_update_record.Why a library reads the attribute instead of calling the reader
For every other Active Record attribute the two are equivalent — including Rails' own
encrypts, where the decryption lives in the attribute's type, so reading the attribute returns the decrypted value. Lockbox is the one case where they differ, which is why it already bridges the gap in[],attributes,attribute_names,inspectandserializable_hash.read_attributeis the one that was left.What it affects
As in #82, anything that goes through an attribute name rather than a method name — asking
has_attribute?and then reading the attribute is a common shape. That is how it reached me: an encrypted attribute rendered blank in a form, and submitting that form wrote the blank back over the ciphertext (railsadminteam/rails_admin#3241).