Skip to content

Fix encrypted attributes improperly casted#1836

Merged
nashby merged 1 commit intoheartcombo:mainfrom
Michaelvilleneuve:fix_encrypted_attributes_improperly_casted
Jul 12, 2024
Merged

Fix encrypted attributes improperly casted#1836
nashby merged 1 commit intoheartcombo:mainfrom
Michaelvilleneuve:fix_encrypted_attributes_improperly_casted

Conversation

@Michaelvilleneuve
Copy link
Copy Markdown
Contributor

This PR fixes a bug introduced by ActiveRecord::Encrypted attributes.
Instead of returning a ActiveModel::Type::String like they used to, encrypted attributes return an instance of ActiveRecord::Encryption::EncryptedAttributeType, on which you need to call #cast_type in order to obtain the original type.

To fix this, I check whether or not the attribute #responds_to?(:cast_type) which would indicate that we need to take into account the underlying type instead of just #type_for_attribute.

Checking this instead of checking if the attribute_name #responds_to?(:encrypted_attributes) allows to take into account other forms of type overriding that ActiveRecord may introduce in the future or potentially already exist.

See the type difference below between a regular string attribute and an encrypted attribute
image

Fixes #1829

@nashby
Copy link
Copy Markdown
Collaborator

nashby commented Jul 12, 2024

@Michaelvilleneuve hey! Thanks for the PR! It looks like Rails bug to me so I've sent a PR fixing it rails/rails#52247 but I think it's ok to merge this for now.

@nashby nashby merged commit c2c7faf into heartcombo:main Jul 12, 2024
@diesl
Copy link
Copy Markdown

diesl commented May 27, 2025

Hi @nashby, could you make a new release please? Quite some time has passed now and I am still waiting for this fix. Thanks!

@carlosantoniodasilva
Copy link
Copy Markdown
Member

@diesl we'll look into a release soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Encrypted attributes always default to textarea

4 participants