Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exception in attr reader without selecting encrypted column #299

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

domcleal
Copy link

When using AR's .select to retrieve a subset of columns for a model,
calling the encrypted attribute reader results in an exception:

ActiveModel::MissingAttributeError: missing attribute: encrypted_street
gems/activerecord-5.1.5/lib/active_record/attribute_methods/read.rb:71:in `block in _read_attribute'
gems/activerecord-5.1.5/lib/active_record/attribute_set.rb:45:in `block in fetch_value'
gems/activerecord-5.1.5/lib/active_record/attribute.rb:219:in `value'
gems/activerecord-5.1.5/lib/active_record/attribute_set.rb:45:in `fetch_value'
gems/activerecord-5.1.5/lib/active_record/attribute_methods/read.rb:71:in `_read_attribute'
gems/activerecord-5.1.5/lib/active_record/attribute_methods/read.rb:36:in `__temp__56e636279707475646f5374727565647'
lib/attr_encrypted.rb:161:in `block (2 levels) in attr_encrypted'

The virtual attribute email is defined in the test, but the reader tries to
access the encrypted column which wasn't selected. This is a problem when
rendering a model #to_json with the default serialiser, as it tries to read
all defined columns.

Allow the reader to return nil in the case when the encrypted attribute
column isn't available.

When using AR's .select to retrieve a subset of columns for a model,
calling the encrypted attribute reader results in an exception:

    ActiveModel::MissingAttributeError: missing attribute: encrypted_street
    gems/activerecord-5.1.5/lib/active_record/attribute_methods/read.rb:71:in `block in _read_attribute'
    gems/activerecord-5.1.5/lib/active_record/attribute_set.rb:45:in `block in fetch_value'
    gems/activerecord-5.1.5/lib/active_record/attribute.rb:219:in `value'
    gems/activerecord-5.1.5/lib/active_record/attribute_set.rb:45:in `fetch_value'
    gems/activerecord-5.1.5/lib/active_record/attribute_methods/read.rb:71:in `_read_attribute'
    gems/activerecord-5.1.5/lib/active_record/attribute_methods/read.rb:36:in `__temp__56e636279707475646f5374727565647'
    lib/attr_encrypted.rb:161:in `block (2 levels) in attr_encrypted'

The virtual attribute `email` is defined in the test, but the reader tries to
access the encrypted column which wasn't selected. This is a problem when
rendering a model `#to_json` with the default serialiser, as it tries to read
all defined columns.

Allow the reader to return nil in the case when the encrypted attribute
column isn't available.
@domcleal domcleal force-pushed the attr-reader-with-limited-columns branch from 65ec470 to 707e6f1 Compare February 20, 2018 11:55
@tadast
Copy link

tadast commented Mar 13, 2018

Would be nice to get it merged, I'm also having the same problem 👍

@oortcloud1996
Copy link

I'm also facing same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants