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

Undefined method #key? for nil:NilClass #423

Closed
TheStranjer opened this issue Nov 8, 2022 · 3 comments · Fixed by #434
Closed

Undefined method #key? for nil:NilClass #423

TheStranjer opened this issue Nov 8, 2022 · 3 comments · Fixed by #434

Comments

@TheStranjer
Copy link

TheStranjer commented Nov 8, 2022

This method crashes on self.class.encrypted_attributes.key?.

def perform_attribute_assignment(method, new_attributes, *args)
  return if new_attributes.blank?

  send method, new_attributes.reject { |k, _|  self.class.encrypted_attributes.key?(k.to_sym) }, *args
  send method, new_attributes.reject { |k, _| !self.class.encrypted_attributes.key?(k.to_sym) }, *args
end

I was using this in a migration when I got this issue.

@movermeyer
Copy link

movermeyer commented Nov 11, 2022

I had a similar errors when trying to upgrade to Rails 7:

NoMethodError: undefined method `key?' for nil:NilClass
 
              send method, new_attributes.reject { |k, _|  self.class.encrypted_attributes.key?(k.to_sym) }, *args
                                                                                          ^^^^^
/tmp/bundle/ruby/3.1.0/gems/attr_encrypted-3.1.0/lib/attr_encrypted/adapters/active_record.rb:28:in `block in perform_attribute_assignment'
/tmp/bundle/ruby/3.1.0/gems/attr_encrypted-3.1.0/lib/attr_encrypted/adapters/active_record.rb:28:in `reject'
/tmp/bundle/ruby/3.1.0/gems/attr_encrypted-3.1.0/lib/attr_encrypted/adapters/active_record.rb:28:in `perform_attribute_assignment'
/tmp/bundle/ruby/3.1.0/gems/attr_encrypted-3.1.0/lib/attr_encrypted/adapters/active_record.rb:36:in `assign_attributes'
/tmp/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/core.rb:468:in `initialize'
/tmp/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/inheritance.rb:75:in `new'
/tmp/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/inheritance.rb:75:in `new'
/tmp/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/relation.rb:862:in `_new'
/tmp/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/relation.rb:71:in `block in new'
/tmp/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/relation.rb:881:in `_scoping'
/tmp/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/relation.rb:428:in `scoping'
/tmp/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/relation.rb:71:in `new'
/tmp/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/relation.rb:227:in `find_or_initialize_by'
/tmp/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/querying.rb:22:in `find_or_initialize_by'
/tmp/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/internal_metadata.rb:31:in `[]='
/tmp/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/schema.rb:63:in `define'
/tmp/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/schema.rb:50:in `define'
/app/db/schema.rb:13:in `<top (required)>'
/tmp/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/tasks/database_tasks.rb:377:in `load'
/tmp/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/tasks/database_tasks.rb:377:in `load_schema'
/tmp/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/tasks/database_tasks.rb:482:in `block in load_schema_current'
/tmp/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/tasks/database_tasks.rb:557:in `block (2 levels) in each_current_configuration'
/tmp/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/tasks/database_tasks.rb:554:in `each'
/tmp/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/tasks/database_tasks.rb:554:in `block in each_current_configuration'
/tmp/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/tasks/database_tasks.rb:553:in `each'
/tmp/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/tasks/database_tasks.rb:553:in `each_current_configuration'
/tmp/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/tasks/database_tasks.rb:481:in `load_schema_current'
/tmp/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/railties/databases.rake:470:in `block (3 levels) in <top (required)>'
/tmp/bundle/ruby/3.1.0/gems/bugsnag-6.24.2/lib/bugsnag/integrations/rake.rb:20:in `execute'
/tmp/bundle/ruby/3.1.0/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
/usr/local/ruby/bin/bundle:25:in `load'
/usr/local/ruby/bin/bundle:25:in `<main>'

Instead of getting set to {} (as it does in Rails 6), encrypted_attributes isn't being set.

@movermeyer
Copy link

movermeyer commented Nov 11, 2022

It seems to collide with Rails 7's new encrypted_attributes class variable.

https://edgeguides.rubyonrails.org/active_record_encryption.html

This was referenced Nov 11, 2022
@erluti
Copy link

erluti commented Jan 24, 2023

I was able to use this blog post to remove attr_encrypted from my rails 7 upgrade of my app
https://pagertree.com/blog/migrate-attr_encrypted-to-rails-7-active-record-encrypts

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