salsify/goldiloader

Rails 5.2 Support

Closed this issue · 6 comments

There are currently two failing tests with Rails 5.2 RC2:

Failures:

  1) Goldiloader when a model is destroyed auto eager loads the associaton on other models
     Failure/Error: ::ActiveRecord::Associations::Preloader.new.preload(models, [association_name])
     
     RuntimeError:
       Can't modify frozen hash
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activemodel-5.2.0.rc2/lib/active_model/attribute_set/builder.rb:44:in `[]='
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activemodel-5.2.0.rc2/lib/active_model/attribute_set.rb:57:in `write_from_user'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/attribute_methods/write.rb:51:in `_write_attribute'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/attribute_methods/write.rb:45:in `write_attribute'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/attribute_methods.rb:410:in `[]='
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/belongs_to_association.rb:92:in `replace_keys'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/belongs_to_association.rb:33:in `target='
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader/association.rb:50:in `associate_records_to_owner'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader/association.rb:26:in `block in run'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader/association.rb:25:in `each'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader/association.rb:25:in `run'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader.rb:142:in `block (2 levels) in preloaders_for_one'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader.rb:140:in `each'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader.rb:140:in `map'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader.rb:140:in `block in preloaders_for_one'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader.rb:139:in `each'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader.rb:139:in `flat_map'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader.rb:139:in `preloaders_for_one'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader.rb:106:in `preloaders_on'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader.rb:93:in `block in preload'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader.rb:92:in `each'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader.rb:92:in `flat_map'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader.rb:92:in `preload'
     # ./lib/goldiloader/association_loader.rb:18:in `eager_load'
     # ./lib/goldiloader/association_loader.rb:12:in `load'
     # ./lib/goldiloader/active_record_patches.rb:113:in `load_with_auto_include'
     # ./lib/goldiloader/active_record_patches.rb:126:in `find_target'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/association.rb:151:in `load_target'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/association.rb:47:in `reload'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/singular_association.rb:9:in `reader'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/builder/association.rb:108:in `blog'
     # ./spec/goldiloader/goldiloader_spec.rb:501:in `block (4 levels) in <top (required)>'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activesupport-5.2.0.rc2/lib/active_support/callbacks.rb:426:in `block in make_lambda'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activesupport-5.2.0.rc2/lib/active_support/callbacks.rb:236:in `block in halting_and_conditional'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activesupport-5.2.0.rc2/lib/active_support/callbacks.rb:517:in `block in invoke_after'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activesupport-5.2.0.rc2/lib/active_support/callbacks.rb:517:in `each'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activesupport-5.2.0.rc2/lib/active_support/callbacks.rb:517:in `invoke_after'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activesupport-5.2.0.rc2/lib/active_support/callbacks.rb:133:in `run_callbacks'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activesupport-5.2.0.rc2/lib/active_support/callbacks.rb:816:in `_run_destroy_callbacks'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/callbacks.rb:323:in `destroy'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/transactions.rb:305:in `block in destroy'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/transactions.rb:386:in `block in with_transaction_returning_status'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/connection_adapters/abstract/database_statements.rb:254:in `block in transaction'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/connection_adapters/abstract/transaction.rb:230:in `block in within_new_transaction'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/connection_adapters/abstract/transaction.rb:227:in `within_new_transaction'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/connection_adapters/abstract/database_statements.rb:254:in `transaction'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/transactions.rb:212:in `transaction'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/transactions.rb:383:in `with_transaction_returning_status'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/transactions.rb:305:in `destroy'
     # ./spec/goldiloader/goldiloader_spec.rb:503:in `block (3 levels) in <top (required)>'

  2) Goldiloader when a model is destroyed can load associations in after_destroy callbacks
     Failure/Error: ::ActiveRecord::Associations::Preloader.new.preload(models, [association_name])
     
     RuntimeError:
       Can't modify frozen hash
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activemodel-5.2.0.rc2/lib/active_model/attribute_set/builder.rb:44:in `[]='
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activemodel-5.2.0.rc2/lib/active_model/attribute_set.rb:57:in `write_from_user'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/attribute_methods/write.rb:51:in `_write_attribute'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/attribute_methods/write.rb:45:in `write_attribute'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/attribute_methods.rb:410:in `[]='
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/belongs_to_association.rb:92:in `replace_keys'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/belongs_to_association.rb:33:in `target='
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader/association.rb:50:in `associate_records_to_owner'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader/association.rb:26:in `block in run'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader/association.rb:25:in `each'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader/association.rb:25:in `run'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader.rb:142:in `block (2 levels) in preloaders_for_one'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader.rb:140:in `each'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader.rb:140:in `map'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader.rb:140:in `block in preloaders_for_one'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader.rb:139:in `each'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader.rb:139:in `flat_map'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader.rb:139:in `preloaders_for_one'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader.rb:106:in `preloaders_on'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader.rb:93:in `block in preload'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader.rb:92:in `each'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader.rb:92:in `flat_map'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/preloader.rb:92:in `preload'
     # ./lib/goldiloader/association_loader.rb:18:in `eager_load'
     # ./lib/goldiloader/association_loader.rb:12:in `load'
     # ./lib/goldiloader/active_record_patches.rb:113:in `load_with_auto_include'
     # ./lib/goldiloader/active_record_patches.rb:126:in `find_target'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/association.rb:151:in `load_target'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/association.rb:47:in `reload'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/singular_association.rb:9:in `reader'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/associations/builder/association.rb:108:in `blog'
     # ./spec/goldiloader/goldiloader_spec.rb:501:in `block (4 levels) in <top (required)>'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activesupport-5.2.0.rc2/lib/active_support/callbacks.rb:426:in `block in make_lambda'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activesupport-5.2.0.rc2/lib/active_support/callbacks.rb:236:in `block in halting_and_conditional'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activesupport-5.2.0.rc2/lib/active_support/callbacks.rb:517:in `block in invoke_after'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activesupport-5.2.0.rc2/lib/active_support/callbacks.rb:517:in `each'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activesupport-5.2.0.rc2/lib/active_support/callbacks.rb:517:in `invoke_after'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activesupport-5.2.0.rc2/lib/active_support/callbacks.rb:133:in `run_callbacks'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activesupport-5.2.0.rc2/lib/active_support/callbacks.rb:816:in `_run_destroy_callbacks'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/callbacks.rb:323:in `destroy'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/transactions.rb:305:in `block in destroy'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/transactions.rb:386:in `block in with_transaction_returning_status'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/connection_adapters/abstract/database_statements.rb:254:in `block in transaction'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/connection_adapters/abstract/transaction.rb:230:in `block in within_new_transaction'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/connection_adapters/abstract/transaction.rb:227:in `within_new_transaction'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/connection_adapters/abstract/database_statements.rb:254:in `transaction'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/transactions.rb:212:in `transaction'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/transactions.rb:383:in `with_transaction_returning_status'
     # /Users/jturkel/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0.rc2/lib/active_record/transactions.rb:305:in `destroy'
     # ./spec/goldiloader/goldiloader_spec.rb:503:in `block (3 levels) in <top (required)>'

Finished in 2.24 seconds (files took 1.05 seconds to load)
75 examples, 2 failures

Failed examples:

rspec ./spec/goldiloader/goldiloader_spec.rb:511 # Goldiloader when a model is destroyed auto eager loads the associaton on other models
rspec ./spec/goldiloader/goldiloader_spec.rb:507 # Goldiloader when a model is destroyed can load associations in after_destroy callbacks

The run in Travis can be found here.

Looks like this broke with rails/rails@a84c765. The fix will be either:

  • Don't perform automatic eager loading if the model has been destroyed.
  • Modify ActiveRecord::Associations::BelongsToAssociation#target= to only call replace_keys if different_target?(record) returns true.

I'll open a Rails issue to see if we can get it fixed in Rails but it's a bit of an obscure case that I'm not sure would hit anyone outside of goldiloader.

rails/rails#32375 has been filed. We'll see what happens and take it from there.

Looks like Rails 5.2.0 was released today without rails/rails#32375. I’ll go ahead and implement the workaround this week.

Rails 5.2.0 is out and stable. Could you upgrade the Rails dependency version to 5.2?

Goldiloader 2.1.0 with Rails 5.2 support has been released.

@jturkel Thanks man!