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 `[]' for nil:NilClass #9

Open
Gaurav2728 opened this issue Sep 11, 2017 · 2 comments
Open

undefined method `[]' for nil:NilClass #9

Gaurav2728 opened this issue Sep 11, 2017 · 2 comments

Comments

@Gaurav2728
Copy link

Gaurav2728 commented Sep 11, 2017

Steps to reproduce the bug:-

Ruby 2.4.1
Rails 4.2.1

Add preferences column to users table.

class AddPreferencesToUser < ActiveRecord::Migration
  def self.up
    add_column :users, :preferences, :text
  end

  def self.down
    remove_column :users, :preferences, :text
  end
end

Add rake task in migration to populate data for preferences column

class PopulateUserPreferences < ActiveRecord::Migration
  def change
    Rake::Task['user:populate_preferences'].invoke
  end
end

user_preferences.rake

namespace :user do
  desc 'Populate user preferences data'
  task populate_preferences: :environment do
    users = User.all
    users.each do |user|
      user.update_attributes(default_country: user.country.id, verified: true)
    end
  end
end

Error logs

undefined method `[]' for nil:NilClass
/Users/gaurav/.rvm/gems/ruby-2.4.1@rails4/gems/serial_preference-1.2.1/lib/serial_preference/has_preference_map.rb:69:in `write_preference_attribute'
/Users/gaurav/.rvm/gems/ruby-2.4.1@rails4/gems/serial_preference-1.2.1/lib/serial_preference/has_preference_map.rb:41:in `block (2 levels) in build_preference_definitions'
/Users/gaurav/.rvm/gems/ruby-2.4.1@rails4/gems/activerecord-4.2.9/lib/active_record/attribute_assignment.rb:54:in `public_send'
/Users/gaurav/.rvm/gems/ruby-2.4.1@rails4/gems/activerecord-4.2.9/lib/active_record/attribute_assignment.rb:54:in `_assign_attribute'
/Users/gaurav/.rvm/gems/ruby-2.4.1@rails4/gems/activerecord-4.2.9/lib/active_record/attribute_assignment.rb:41:in `block in assign_attributes'
/Users/gaurav/.rvm/gems/ruby-2.4.1@rails4/gems/activerecord-4.2.9/lib/active_record/attribute_assignment.rb:35:in `each'
/Users/gaurav/.rvm/gems/ruby-2.4.1@rails4/gems/activerecord-4.2.9/lib/active_record/attribute_assignment.rb:35:in `assign_attributes'
/Users/gaurav/.rvm/gems/ruby-2.4.1@rails4/gems/activerecord-4.2.9/lib/active_record/persistence.rb:251:in `block in update'
/Users/gaurav/.rvm/gems/ruby-2.4.1@rails4/gems/activerecord-4.2.9/lib/active_record/transactions.rb:351:in `block in with_transaction_returning_status'
/Users/gaurav/.rvm/gems/ruby-2.4.1@rails4/gems/activerecord-4.2.9/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/Users/gaurav/.rvm/gems/ruby-2.4.1@rails4/gems/activerecord-4.2.9/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/Users/gaurav/.rvm/gems/ruby-2.4.1@rails4/gems/activerecord-4.2.9/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/Users/gaurav/.rvm/gems/ruby-2.4.1@rails4/gems/activerecord-4.2.9/lib/active_record/transactions.rb:220:in `transaction'
/Users/gaurav/.rvm/gems/ruby-2.4.1@rails4/gems/activerecord-4.2.9/lib/active_record/transactions.rb:348:in `with_transaction_returning_status'
/Users/gaurav/.rvm/gems/ruby-2.4.1@rails4/gems/activerecord-4.2.9/lib/active_record/persistence.rb:250:in `update'

When we re-run the migration it'll successfully run. This is a race condition when preferences will not set and it'll fall back but re-run the migration found the preferences and run successfully in 2nd time.

@asanghi
Copy link
Owner

asanghi commented Sep 11, 2017

How are preferences defined in user model?

@Gaurav2728
Copy link
Author

Gaurav2728 commented Sep 11, 2017

preferences do
  integer :default_country, default: 1
  boolean :verified, default: true
end

Gaurav2728 pushed a commit to Gaurav2728/serial_preference that referenced this issue Sep 11, 2017
asanghi#9

After adding preferences column using migration and also populate data using migration preferences column not updated with HashWithIndifferentAccess.new 

We need to reset the self.class.reset_column_information and set preferences as a Hash
Now preferences column able to write/store all preferences attributes
Gaurav2728 added a commit to Gaurav2728/serial_preference that referenced this issue Sep 11, 2017
asanghi#9

After adding preferences column using migration and also populate data using migration preferences column not updated with HashWithIndifferentAccess.new 

We need to reset the self.class.reset_column_information and set preferences as a Hash
Now preferences column able to write/store all preferences attributes
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

No branches or pull requests

2 participants