activerecord 6.1.6 → 7.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- checksums.yaml +4 -4
 - data/CHANGELOG.md +1627 -983
 - data/MIT-LICENSE +1 -1
 - data/README.rdoc +18 -18
 - data/lib/active_record/aggregations.rb +17 -14
 - data/lib/active_record/association_relation.rb +1 -11
 - data/lib/active_record/associations/association.rb +50 -19
 - data/lib/active_record/associations/association_scope.rb +17 -12
 - data/lib/active_record/associations/belongs_to_association.rb +28 -9
 - data/lib/active_record/associations/belongs_to_polymorphic_association.rb +10 -2
 - data/lib/active_record/associations/builder/association.rb +11 -5
 - data/lib/active_record/associations/builder/belongs_to.rb +40 -14
 - data/lib/active_record/associations/builder/collection_association.rb +10 -3
 - data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +1 -5
 - data/lib/active_record/associations/builder/has_many.rb +3 -2
 - data/lib/active_record/associations/builder/has_one.rb +2 -1
 - data/lib/active_record/associations/builder/singular_association.rb +6 -2
 - data/lib/active_record/associations/collection_association.rb +35 -31
 - data/lib/active_record/associations/collection_proxy.rb +30 -15
 - data/lib/active_record/associations/disable_joins_association_scope.rb +59 -0
 - data/lib/active_record/associations/foreign_association.rb +10 -3
 - data/lib/active_record/associations/has_many_association.rb +28 -18
 - data/lib/active_record/associations/has_many_through_association.rb +12 -7
 - data/lib/active_record/associations/has_one_association.rb +20 -10
 - data/lib/active_record/associations/has_one_through_association.rb +1 -1
 - data/lib/active_record/associations/join_dependency.rb +26 -16
 - data/lib/active_record/associations/preloader/association.rb +207 -52
 - data/lib/active_record/associations/preloader/batch.rb +48 -0
 - data/lib/active_record/associations/preloader/branch.rb +147 -0
 - data/lib/active_record/associations/preloader/through_association.rb +50 -14
 - data/lib/active_record/associations/preloader.rb +50 -121
 - data/lib/active_record/associations/singular_association.rb +9 -3
 - data/lib/active_record/associations/through_association.rb +25 -14
 - data/lib/active_record/associations.rb +439 -305
 - data/lib/active_record/asynchronous_queries_tracker.rb +60 -0
 - data/lib/active_record/attribute_assignment.rb +1 -3
 - data/lib/active_record/attribute_methods/before_type_cast.rb +24 -2
 - data/lib/active_record/attribute_methods/dirty.rb +73 -22
 - data/lib/active_record/attribute_methods/primary_key.rb +78 -26
 - data/lib/active_record/attribute_methods/query.rb +31 -19
 - data/lib/active_record/attribute_methods/read.rb +25 -10
 - data/lib/active_record/attribute_methods/serialization.rb +194 -37
 - data/lib/active_record/attribute_methods/time_zone_conversion.rb +4 -3
 - data/lib/active_record/attribute_methods/write.rb +10 -13
 - data/lib/active_record/attribute_methods.rb +121 -40
 - data/lib/active_record/attributes.rb +27 -38
 - data/lib/active_record/autosave_association.rb +61 -30
 - data/lib/active_record/base.rb +25 -2
 - data/lib/active_record/callbacks.rb +18 -34
 - data/lib/active_record/coders/column_serializer.rb +61 -0
 - data/lib/active_record/coders/json.rb +1 -1
 - data/lib/active_record/coders/yaml_column.rb +70 -34
 - data/lib/active_record/connection_adapters/abstract/connection_handler.rb +367 -0
 - data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +211 -0
 - data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +78 -0
 - data/lib/active_record/connection_adapters/abstract/connection_pool.rb +96 -590
 - data/lib/active_record/connection_adapters/abstract/database_limits.rb +5 -17
 - data/lib/active_record/connection_adapters/abstract/database_statements.rb +172 -50
 - data/lib/active_record/connection_adapters/abstract/query_cache.rb +77 -27
 - data/lib/active_record/connection_adapters/abstract/quoting.rb +87 -73
 - data/lib/active_record/connection_adapters/abstract/savepoints.rb +4 -3
 - data/lib/active_record/connection_adapters/abstract/schema_creation.rb +21 -20
 - data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +186 -31
 - data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +14 -1
 - data/lib/active_record/connection_adapters/abstract/schema_statements.rb +360 -138
 - data/lib/active_record/connection_adapters/abstract/transaction.rb +281 -59
 - data/lib/active_record/connection_adapters/abstract_adapter.rb +631 -149
 - data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +285 -156
 - data/lib/active_record/connection_adapters/column.rb +13 -0
 - data/lib/active_record/connection_adapters/mysql/column.rb +1 -0
 - data/lib/active_record/connection_adapters/mysql/database_statements.rb +25 -134
 - data/lib/active_record/connection_adapters/mysql/quoting.rb +56 -25
 - data/lib/active_record/connection_adapters/mysql/schema_creation.rb +9 -0
 - data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +10 -1
 - data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +8 -2
 - data/lib/active_record/connection_adapters/mysql/schema_statements.rb +38 -14
 - data/lib/active_record/connection_adapters/mysql2/database_statements.rb +151 -0
 - data/lib/active_record/connection_adapters/mysql2_adapter.rb +104 -53
 - data/lib/active_record/connection_adapters/pool_config.rb +20 -11
 - data/lib/active_record/connection_adapters/pool_manager.rb +19 -9
 - data/lib/active_record/connection_adapters/postgresql/column.rb +30 -1
 - data/lib/active_record/connection_adapters/postgresql/database_statements.rb +89 -52
 - data/lib/active_record/connection_adapters/postgresql/oid/array.rb +1 -1
 - data/lib/active_record/connection_adapters/postgresql/oid/date.rb +8 -0
 - data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +5 -0
 - data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +53 -14
 - data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -2
 - data/lib/active_record/connection_adapters/postgresql/oid/range.rb +12 -3
 - data/lib/active_record/connection_adapters/postgresql/oid/timestamp.rb +15 -0
 - data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +30 -0
 - data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +18 -6
 - data/lib/active_record/connection_adapters/postgresql/oid.rb +2 -0
 - data/lib/active_record/connection_adapters/postgresql/quoting.rb +89 -56
 - data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +28 -0
 - data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +92 -2
 - data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +153 -3
 - data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +78 -0
 - data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +394 -74
 - data/lib/active_record/connection_adapters/postgresql/utils.rb +9 -10
 - data/lib/active_record/connection_adapters/postgresql_adapter.rb +509 -247
 - data/lib/active_record/connection_adapters/schema_cache.rb +319 -90
 - data/lib/active_record/connection_adapters/sqlite3/column.rb +49 -0
 - data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +72 -53
 - data/lib/active_record/connection_adapters/sqlite3/quoting.rb +37 -21
 - data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +7 -0
 - data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +43 -22
 - data/lib/active_record/connection_adapters/sqlite3_adapter.rb +294 -102
 - data/lib/active_record/connection_adapters/statement_pool.rb +7 -0
 - data/lib/active_record/connection_adapters/trilogy/database_statements.rb +99 -0
 - data/lib/active_record/connection_adapters/trilogy_adapter.rb +254 -0
 - data/lib/active_record/connection_adapters.rb +9 -6
 - data/lib/active_record/connection_handling.rb +107 -136
 - data/lib/active_record/core.rb +202 -223
 - data/lib/active_record/counter_cache.rb +46 -25
 - data/lib/active_record/database_configurations/connection_url_resolver.rb +2 -1
 - data/lib/active_record/database_configurations/database_config.rb +21 -12
 - data/lib/active_record/database_configurations/hash_config.rb +84 -16
 - data/lib/active_record/database_configurations/url_config.rb +18 -12
 - data/lib/active_record/database_configurations.rb +95 -59
 - data/lib/active_record/delegated_type.rb +61 -15
 - data/lib/active_record/deprecator.rb +7 -0
 - data/lib/active_record/destroy_association_async_job.rb +3 -1
 - data/lib/active_record/disable_joins_association_relation.rb +39 -0
 - data/lib/active_record/dynamic_matchers.rb +1 -1
 - data/lib/active_record/encryption/auto_filtered_parameters.rb +66 -0
 - data/lib/active_record/encryption/cipher/aes256_gcm.rb +101 -0
 - data/lib/active_record/encryption/cipher.rb +53 -0
 - data/lib/active_record/encryption/config.rb +68 -0
 - data/lib/active_record/encryption/configurable.rb +60 -0
 - data/lib/active_record/encryption/context.rb +42 -0
 - data/lib/active_record/encryption/contexts.rb +76 -0
 - data/lib/active_record/encryption/derived_secret_key_provider.rb +18 -0
 - data/lib/active_record/encryption/deterministic_key_provider.rb +14 -0
 - data/lib/active_record/encryption/encryptable_record.rb +224 -0
 - data/lib/active_record/encryption/encrypted_attribute_type.rb +151 -0
 - data/lib/active_record/encryption/encrypted_fixtures.rb +38 -0
 - data/lib/active_record/encryption/encrypting_only_encryptor.rb +12 -0
 - data/lib/active_record/encryption/encryptor.rb +155 -0
 - data/lib/active_record/encryption/envelope_encryption_key_provider.rb +55 -0
 - data/lib/active_record/encryption/errors.rb +15 -0
 - data/lib/active_record/encryption/extended_deterministic_queries.rb +157 -0
 - data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +28 -0
 - data/lib/active_record/encryption/key.rb +28 -0
 - data/lib/active_record/encryption/key_generator.rb +53 -0
 - data/lib/active_record/encryption/key_provider.rb +46 -0
 - data/lib/active_record/encryption/message.rb +33 -0
 - data/lib/active_record/encryption/message_serializer.rb +92 -0
 - data/lib/active_record/encryption/null_encryptor.rb +21 -0
 - data/lib/active_record/encryption/properties.rb +76 -0
 - data/lib/active_record/encryption/read_only_null_encryptor.rb +24 -0
 - data/lib/active_record/encryption/scheme.rb +96 -0
 - data/lib/active_record/encryption.rb +56 -0
 - data/lib/active_record/enum.rb +154 -63
 - data/lib/active_record/errors.rb +171 -15
 - data/lib/active_record/explain.rb +23 -3
 - data/lib/active_record/explain_registry.rb +11 -6
 - data/lib/active_record/explain_subscriber.rb +1 -1
 - data/lib/active_record/fixture_set/file.rb +15 -1
 - data/lib/active_record/fixture_set/model_metadata.rb +14 -4
 - data/lib/active_record/fixture_set/render_context.rb +2 -0
 - data/lib/active_record/fixture_set/table_row.rb +70 -14
 - data/lib/active_record/fixture_set/table_rows.rb +4 -4
 - data/lib/active_record/fixtures.rb +131 -86
 - data/lib/active_record/future_result.rb +164 -0
 - data/lib/active_record/gem_version.rb +3 -3
 - data/lib/active_record/inheritance.rb +81 -29
 - data/lib/active_record/insert_all.rb +135 -22
 - data/lib/active_record/integration.rb +11 -10
 - data/lib/active_record/internal_metadata.rb +119 -33
 - data/lib/active_record/legacy_yaml_adapter.rb +2 -39
 - data/lib/active_record/locking/optimistic.rb +36 -21
 - data/lib/active_record/locking/pessimistic.rb +15 -6
 - data/lib/active_record/log_subscriber.rb +52 -19
 - data/lib/active_record/marshalling.rb +56 -0
 - data/lib/active_record/message_pack.rb +124 -0
 - data/lib/active_record/middleware/database_selector/resolver.rb +10 -10
 - data/lib/active_record/middleware/database_selector.rb +23 -13
 - data/lib/active_record/middleware/shard_selector.rb +62 -0
 - data/lib/active_record/migration/command_recorder.rb +112 -14
 - data/lib/active_record/migration/compatibility.rb +221 -48
 - data/lib/active_record/migration/default_strategy.rb +23 -0
 - data/lib/active_record/migration/execution_strategy.rb +19 -0
 - data/lib/active_record/migration/join_table.rb +1 -1
 - data/lib/active_record/migration/pending_migration_connection.rb +21 -0
 - data/lib/active_record/migration.rb +358 -171
 - data/lib/active_record/model_schema.rb +120 -101
 - data/lib/active_record/nested_attributes.rb +37 -18
 - data/lib/active_record/no_touching.rb +3 -3
 - data/lib/active_record/normalization.rb +167 -0
 - data/lib/active_record/persistence.rb +405 -85
 - data/lib/active_record/promise.rb +84 -0
 - data/lib/active_record/query_cache.rb +3 -21
 - data/lib/active_record/query_logs.rb +174 -0
 - data/lib/active_record/query_logs_formatter.rb +41 -0
 - data/lib/active_record/querying.rb +29 -6
 - data/lib/active_record/railtie.rb +219 -43
 - data/lib/active_record/railties/controller_runtime.rb +13 -9
 - data/lib/active_record/railties/databases.rake +188 -252
 - data/lib/active_record/railties/job_runtime.rb +23 -0
 - data/lib/active_record/readonly_attributes.rb +41 -3
 - data/lib/active_record/reflection.rb +241 -80
 - data/lib/active_record/relation/batches/batch_enumerator.rb +23 -7
 - data/lib/active_record/relation/batches.rb +192 -63
 - data/lib/active_record/relation/calculations.rb +219 -90
 - data/lib/active_record/relation/delegation.rb +27 -13
 - data/lib/active_record/relation/finder_methods.rb +108 -51
 - data/lib/active_record/relation/merger.rb +22 -13
 - data/lib/active_record/relation/predicate_builder/association_query_value.rb +31 -3
 - data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +4 -6
 - data/lib/active_record/relation/predicate_builder/relation_handler.rb +5 -1
 - data/lib/active_record/relation/predicate_builder.rb +27 -20
 - data/lib/active_record/relation/query_attribute.rb +30 -12
 - data/lib/active_record/relation/query_methods.rb +654 -127
 - data/lib/active_record/relation/record_fetch_warning.rb +7 -9
 - data/lib/active_record/relation/spawn_methods.rb +20 -3
 - data/lib/active_record/relation/where_clause.rb +10 -19
 - data/lib/active_record/relation.rb +262 -120
 - data/lib/active_record/result.rb +37 -11
 - data/lib/active_record/runtime_registry.rb +18 -13
 - data/lib/active_record/sanitization.rb +65 -20
 - data/lib/active_record/schema.rb +36 -22
 - data/lib/active_record/schema_dumper.rb +73 -24
 - data/lib/active_record/schema_migration.rb +68 -33
 - data/lib/active_record/scoping/default.rb +72 -15
 - data/lib/active_record/scoping/named.rb +5 -13
 - data/lib/active_record/scoping.rb +65 -34
 - data/lib/active_record/secure_password.rb +60 -0
 - data/lib/active_record/secure_token.rb +21 -3
 - data/lib/active_record/serialization.rb +6 -1
 - data/lib/active_record/signed_id.rb +10 -8
 - data/lib/active_record/store.rb +16 -11
 - data/lib/active_record/suppressor.rb +13 -15
 - data/lib/active_record/table_metadata.rb +16 -3
 - data/lib/active_record/tasks/database_tasks.rb +225 -136
 - data/lib/active_record/tasks/mysql_database_tasks.rb +16 -7
 - data/lib/active_record/tasks/postgresql_database_tasks.rb +35 -26
 - data/lib/active_record/tasks/sqlite_database_tasks.rb +15 -7
 - data/lib/active_record/test_databases.rb +1 -1
 - data/lib/active_record/test_fixtures.rb +123 -99
 - data/lib/active_record/timestamp.rb +29 -18
 - data/lib/active_record/token_for.rb +113 -0
 - data/lib/active_record/touch_later.rb +11 -6
 - data/lib/active_record/transactions.rb +48 -27
 - data/lib/active_record/translation.rb +3 -3
 - data/lib/active_record/type/adapter_specific_registry.rb +32 -14
 - data/lib/active_record/type/hash_lookup_type_map.rb +34 -1
 - data/lib/active_record/type/internal/timezone.rb +7 -2
 - data/lib/active_record/type/serialized.rb +9 -5
 - data/lib/active_record/type/time.rb +4 -0
 - data/lib/active_record/type/type_map.rb +17 -20
 - data/lib/active_record/type.rb +1 -2
 - data/lib/active_record/validations/absence.rb +1 -1
 - data/lib/active_record/validations/associated.rb +4 -4
 - data/lib/active_record/validations/numericality.rb +5 -4
 - data/lib/active_record/validations/presence.rb +5 -28
 - data/lib/active_record/validations/uniqueness.rb +51 -6
 - data/lib/active_record/validations.rb +8 -4
 - data/lib/active_record/version.rb +1 -1
 - data/lib/active_record.rb +335 -32
 - data/lib/arel/attributes/attribute.rb +0 -8
 - data/lib/arel/crud.rb +28 -22
 - data/lib/arel/delete_manager.rb +18 -4
 - data/lib/arel/errors.rb +10 -0
 - data/lib/arel/factory_methods.rb +4 -0
 - data/lib/arel/filter_predications.rb +9 -0
 - data/lib/arel/insert_manager.rb +2 -3
 - data/lib/arel/nodes/and.rb +4 -0
 - data/lib/arel/nodes/binary.rb +6 -1
 - data/lib/arel/nodes/bound_sql_literal.rb +61 -0
 - data/lib/arel/nodes/casted.rb +1 -1
 - data/lib/arel/nodes/cte.rb +36 -0
 - data/lib/arel/nodes/delete_statement.rb +12 -13
 - data/lib/arel/nodes/filter.rb +10 -0
 - data/lib/arel/nodes/fragments.rb +35 -0
 - data/lib/arel/nodes/function.rb +1 -0
 - data/lib/arel/nodes/homogeneous_in.rb +1 -9
 - data/lib/arel/nodes/insert_statement.rb +2 -2
 - data/lib/arel/nodes/leading_join.rb +8 -0
 - data/lib/arel/nodes/node.rb +111 -2
 - data/lib/arel/nodes/select_core.rb +2 -2
 - data/lib/arel/nodes/select_statement.rb +2 -2
 - data/lib/arel/nodes/sql_literal.rb +6 -0
 - data/lib/arel/nodes/table_alias.rb +4 -0
 - data/lib/arel/nodes/update_statement.rb +8 -3
 - data/lib/arel/nodes.rb +5 -0
 - data/lib/arel/predications.rb +13 -3
 - data/lib/arel/select_manager.rb +10 -4
 - data/lib/arel/table.rb +9 -6
 - data/lib/arel/tree_manager.rb +0 -12
 - data/lib/arel/update_manager.rb +18 -4
 - data/lib/arel/visitors/dot.rb +80 -90
 - data/lib/arel/visitors/mysql.rb +16 -3
 - data/lib/arel/visitors/postgresql.rb +0 -10
 - data/lib/arel/visitors/to_sql.rb +139 -19
 - data/lib/arel/visitors/visitor.rb +2 -2
 - data/lib/arel.rb +18 -3
 - data/lib/rails/generators/active_record/application_record/USAGE +8 -0
 - data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +1 -1
 - data/lib/rails/generators/active_record/migration.rb +3 -1
 - data/lib/rails/generators/active_record/model/USAGE +113 -0
 - data/lib/rails/generators/active_record/model/model_generator.rb +15 -6
 - data/lib/rails/generators/active_record/model/templates/abstract_base_class.rb.tt +1 -1
 - data/lib/rails/generators/active_record/model/templates/model.rb.tt +1 -1
 - data/lib/rails/generators/active_record/model/templates/module.rb.tt +2 -2
 - data/lib/rails/generators/active_record/multi_db/multi_db_generator.rb +16 -0
 - data/lib/rails/generators/active_record/multi_db/templates/multi_db.rb.tt +44 -0
 - metadata +93 -13
 - data/lib/active_record/connection_adapters/legacy_pool_manager.rb +0 -35
 - data/lib/active_record/null_relation.rb +0 -67
 
| 
         @@ -0,0 +1,147 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            module ActiveRecord
         
     | 
| 
      
 4 
     | 
    
         
            +
              module Associations
         
     | 
| 
      
 5 
     | 
    
         
            +
                class Preloader
         
     | 
| 
      
 6 
     | 
    
         
            +
                  class Branch # :nodoc:
         
     | 
| 
      
 7 
     | 
    
         
            +
                    attr_reader :association, :children, :parent
         
     | 
| 
      
 8 
     | 
    
         
            +
                    attr_reader :scope, :associate_by_default
         
     | 
| 
      
 9 
     | 
    
         
            +
                    attr_writer :preloaded_records
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
                    def initialize(association:, children:, parent:, associate_by_default:, scope:)
         
     | 
| 
      
 12 
     | 
    
         
            +
                      @association = association
         
     | 
| 
      
 13 
     | 
    
         
            +
                      @parent = parent
         
     | 
| 
      
 14 
     | 
    
         
            +
                      @scope = scope
         
     | 
| 
      
 15 
     | 
    
         
            +
                      @associate_by_default = associate_by_default
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                      @children = build_children(children)
         
     | 
| 
      
 18 
     | 
    
         
            +
                      @loaders = nil
         
     | 
| 
      
 19 
     | 
    
         
            +
                    end
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
                    def future_classes
         
     | 
| 
      
 22 
     | 
    
         
            +
                      (immediate_future_classes + children.flat_map(&:future_classes)).uniq
         
     | 
| 
      
 23 
     | 
    
         
            +
                    end
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
                    def immediate_future_classes
         
     | 
| 
      
 26 
     | 
    
         
            +
                      if parent.done?
         
     | 
| 
      
 27 
     | 
    
         
            +
                        loaders.flat_map(&:future_classes).uniq
         
     | 
| 
      
 28 
     | 
    
         
            +
                      else
         
     | 
| 
      
 29 
     | 
    
         
            +
                        likely_reflections.reject(&:polymorphic?).flat_map do |reflection|
         
     | 
| 
      
 30 
     | 
    
         
            +
                          reflection.
         
     | 
| 
      
 31 
     | 
    
         
            +
                            chain.
         
     | 
| 
      
 32 
     | 
    
         
            +
                            map(&:klass)
         
     | 
| 
      
 33 
     | 
    
         
            +
                        end.uniq
         
     | 
| 
      
 34 
     | 
    
         
            +
                      end
         
     | 
| 
      
 35 
     | 
    
         
            +
                    end
         
     | 
| 
      
 36 
     | 
    
         
            +
             
     | 
| 
      
 37 
     | 
    
         
            +
                    def target_classes
         
     | 
| 
      
 38 
     | 
    
         
            +
                      if done?
         
     | 
| 
      
 39 
     | 
    
         
            +
                        preloaded_records.map(&:klass).uniq
         
     | 
| 
      
 40 
     | 
    
         
            +
                      elsif parent.done?
         
     | 
| 
      
 41 
     | 
    
         
            +
                        loaders.map(&:klass).uniq
         
     | 
| 
      
 42 
     | 
    
         
            +
                      else
         
     | 
| 
      
 43 
     | 
    
         
            +
                        likely_reflections.reject(&:polymorphic?).map(&:klass).uniq
         
     | 
| 
      
 44 
     | 
    
         
            +
                      end
         
     | 
| 
      
 45 
     | 
    
         
            +
                    end
         
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
      
 47 
     | 
    
         
            +
                    def likely_reflections
         
     | 
| 
      
 48 
     | 
    
         
            +
                      parent_classes = parent.target_classes
         
     | 
| 
      
 49 
     | 
    
         
            +
                      parent_classes.filter_map do |parent_klass|
         
     | 
| 
      
 50 
     | 
    
         
            +
                        parent_klass._reflect_on_association(@association)
         
     | 
| 
      
 51 
     | 
    
         
            +
                      end
         
     | 
| 
      
 52 
     | 
    
         
            +
                    end
         
     | 
| 
      
 53 
     | 
    
         
            +
             
     | 
| 
      
 54 
     | 
    
         
            +
                    def root?
         
     | 
| 
      
 55 
     | 
    
         
            +
                      parent.nil?
         
     | 
| 
      
 56 
     | 
    
         
            +
                    end
         
     | 
| 
      
 57 
     | 
    
         
            +
             
     | 
| 
      
 58 
     | 
    
         
            +
                    def source_records
         
     | 
| 
      
 59 
     | 
    
         
            +
                      @parent.preloaded_records
         
     | 
| 
      
 60 
     | 
    
         
            +
                    end
         
     | 
| 
      
 61 
     | 
    
         
            +
             
     | 
| 
      
 62 
     | 
    
         
            +
                    def preloaded_records
         
     | 
| 
      
 63 
     | 
    
         
            +
                      @preloaded_records ||= loaders.flat_map(&:preloaded_records)
         
     | 
| 
      
 64 
     | 
    
         
            +
                    end
         
     | 
| 
      
 65 
     | 
    
         
            +
             
     | 
| 
      
 66 
     | 
    
         
            +
                    def done?
         
     | 
| 
      
 67 
     | 
    
         
            +
                      root? || (@loaders && @loaders.all?(&:run?))
         
     | 
| 
      
 68 
     | 
    
         
            +
                    end
         
     | 
| 
      
 69 
     | 
    
         
            +
             
     | 
| 
      
 70 
     | 
    
         
            +
                    def runnable_loaders
         
     | 
| 
      
 71 
     | 
    
         
            +
                      loaders.flat_map(&:runnable_loaders).reject(&:run?)
         
     | 
| 
      
 72 
     | 
    
         
            +
                    end
         
     | 
| 
      
 73 
     | 
    
         
            +
             
     | 
| 
      
 74 
     | 
    
         
            +
                    def grouped_records
         
     | 
| 
      
 75 
     | 
    
         
            +
                      h = {}
         
     | 
| 
      
 76 
     | 
    
         
            +
                      polymorphic_parent = !root? && parent.polymorphic?
         
     | 
| 
      
 77 
     | 
    
         
            +
                      source_records.each do |record|
         
     | 
| 
      
 78 
     | 
    
         
            +
                        reflection = record.class._reflect_on_association(association)
         
     | 
| 
      
 79 
     | 
    
         
            +
                        next if polymorphic_parent && !reflection || !record.association(association).klass
         
     | 
| 
      
 80 
     | 
    
         
            +
                        (h[reflection] ||= []) << record
         
     | 
| 
      
 81 
     | 
    
         
            +
                      end
         
     | 
| 
      
 82 
     | 
    
         
            +
                      h
         
     | 
| 
      
 83 
     | 
    
         
            +
                    end
         
     | 
| 
      
 84 
     | 
    
         
            +
             
     | 
| 
      
 85 
     | 
    
         
            +
                    def preloaders_for_reflection(reflection, reflection_records)
         
     | 
| 
      
 86 
     | 
    
         
            +
                      reflection_records.group_by do |record|
         
     | 
| 
      
 87 
     | 
    
         
            +
                        klass = record.association(association).klass
         
     | 
| 
      
 88 
     | 
    
         
            +
             
     | 
| 
      
 89 
     | 
    
         
            +
                        if reflection.scope && reflection.scope.arity != 0
         
     | 
| 
      
 90 
     | 
    
         
            +
                          # For instance dependent scopes, the scope is potentially
         
     | 
| 
      
 91 
     | 
    
         
            +
                          # different for each record. To allow this we'll group each
         
     | 
| 
      
 92 
     | 
    
         
            +
                          # object separately into its own preloader
         
     | 
| 
      
 93 
     | 
    
         
            +
                          reflection_scope = reflection.join_scopes(klass.arel_table, klass.predicate_builder, klass, record).inject(&:merge!)
         
     | 
| 
      
 94 
     | 
    
         
            +
                        end
         
     | 
| 
      
 95 
     | 
    
         
            +
             
     | 
| 
      
 96 
     | 
    
         
            +
                        [klass, reflection_scope]
         
     | 
| 
      
 97 
     | 
    
         
            +
                      end.map do |(rhs_klass, reflection_scope), rs|
         
     | 
| 
      
 98 
     | 
    
         
            +
                        preloader_for(reflection).new(rhs_klass, rs, reflection, scope, reflection_scope, associate_by_default)
         
     | 
| 
      
 99 
     | 
    
         
            +
                      end
         
     | 
| 
      
 100 
     | 
    
         
            +
                    end
         
     | 
| 
      
 101 
     | 
    
         
            +
             
     | 
| 
      
 102 
     | 
    
         
            +
                    def polymorphic?
         
     | 
| 
      
 103 
     | 
    
         
            +
                      return false if root?
         
     | 
| 
      
 104 
     | 
    
         
            +
                      return @polymorphic if defined?(@polymorphic)
         
     | 
| 
      
 105 
     | 
    
         
            +
             
     | 
| 
      
 106 
     | 
    
         
            +
                      @polymorphic = source_records.any? do |record|
         
     | 
| 
      
 107 
     | 
    
         
            +
                        reflection = record.class._reflect_on_association(association)
         
     | 
| 
      
 108 
     | 
    
         
            +
                        reflection && reflection.options[:polymorphic]
         
     | 
| 
      
 109 
     | 
    
         
            +
                      end
         
     | 
| 
      
 110 
     | 
    
         
            +
                    end
         
     | 
| 
      
 111 
     | 
    
         
            +
             
     | 
| 
      
 112 
     | 
    
         
            +
                    def loaders
         
     | 
| 
      
 113 
     | 
    
         
            +
                      @loaders ||=
         
     | 
| 
      
 114 
     | 
    
         
            +
                        grouped_records.flat_map do |reflection, reflection_records|
         
     | 
| 
      
 115 
     | 
    
         
            +
                          preloaders_for_reflection(reflection, reflection_records)
         
     | 
| 
      
 116 
     | 
    
         
            +
                        end
         
     | 
| 
      
 117 
     | 
    
         
            +
                    end
         
     | 
| 
      
 118 
     | 
    
         
            +
             
     | 
| 
      
 119 
     | 
    
         
            +
                    private
         
     | 
| 
      
 120 
     | 
    
         
            +
                      def build_children(children)
         
     | 
| 
      
 121 
     | 
    
         
            +
                        Array.wrap(children).flat_map { |association|
         
     | 
| 
      
 122 
     | 
    
         
            +
                          Array(association).flat_map { |parent, child|
         
     | 
| 
      
 123 
     | 
    
         
            +
                            Branch.new(
         
     | 
| 
      
 124 
     | 
    
         
            +
                              parent: self,
         
     | 
| 
      
 125 
     | 
    
         
            +
                              association: parent,
         
     | 
| 
      
 126 
     | 
    
         
            +
                              children: child,
         
     | 
| 
      
 127 
     | 
    
         
            +
                              associate_by_default: associate_by_default,
         
     | 
| 
      
 128 
     | 
    
         
            +
                              scope: scope
         
     | 
| 
      
 129 
     | 
    
         
            +
                            )
         
     | 
| 
      
 130 
     | 
    
         
            +
                          }
         
     | 
| 
      
 131 
     | 
    
         
            +
                        }
         
     | 
| 
      
 132 
     | 
    
         
            +
                      end
         
     | 
| 
      
 133 
     | 
    
         
            +
             
     | 
| 
      
 134 
     | 
    
         
            +
                      # Returns a class containing the logic needed to load preload the data
         
     | 
| 
      
 135 
     | 
    
         
            +
                      # and attach it to a relation. The class returned implements a `run` method
         
     | 
| 
      
 136 
     | 
    
         
            +
                      # that accepts a preloader.
         
     | 
| 
      
 137 
     | 
    
         
            +
                      def preloader_for(reflection)
         
     | 
| 
      
 138 
     | 
    
         
            +
                        if reflection.options[:through]
         
     | 
| 
      
 139 
     | 
    
         
            +
                          ThroughAssociation
         
     | 
| 
      
 140 
     | 
    
         
            +
                        else
         
     | 
| 
      
 141 
     | 
    
         
            +
                          Association
         
     | 
| 
      
 142 
     | 
    
         
            +
                        end
         
     | 
| 
      
 143 
     | 
    
         
            +
                      end
         
     | 
| 
      
 144 
     | 
    
         
            +
                  end
         
     | 
| 
      
 145 
     | 
    
         
            +
                end
         
     | 
| 
      
 146 
     | 
    
         
            +
              end
         
     | 
| 
      
 147 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -4,26 +4,22 @@ module ActiveRecord 
     | 
|
| 
       4 
4 
     | 
    
         
             
              module Associations
         
     | 
| 
       5 
5 
     | 
    
         
             
                class Preloader
         
     | 
| 
       6 
6 
     | 
    
         
             
                  class ThroughAssociation < Association # :nodoc:
         
     | 
| 
       7 
     | 
    
         
            -
                    PRELOADER = ActiveRecord::Associations::Preloader.new(associate_by_default: false)
         
     | 
| 
       8 
     | 
    
         
            -
             
     | 
| 
       9 
     | 
    
         
            -
                    def initialize(*)
         
     | 
| 
       10 
     | 
    
         
            -
                      super
         
     | 
| 
       11 
     | 
    
         
            -
                      @already_loaded = owners.first.association(through_reflection.name).loaded?
         
     | 
| 
       12 
     | 
    
         
            -
                    end
         
     | 
| 
       13 
     | 
    
         
            -
             
     | 
| 
       14 
7 
     | 
    
         
             
                    def preloaded_records
         
     | 
| 
       15 
8 
     | 
    
         
             
                      @preloaded_records ||= source_preloaders.flat_map(&:preloaded_records)
         
     | 
| 
       16 
9 
     | 
    
         
             
                    end
         
     | 
| 
       17 
10 
     | 
    
         | 
| 
       18 
11 
     | 
    
         
             
                    def records_by_owner
         
     | 
| 
       19 
12 
     | 
    
         
             
                      return @records_by_owner if defined?(@records_by_owner)
         
     | 
| 
       20 
     | 
    
         
            -
                      source_records_by_owner = source_preloaders.map(&:records_by_owner).reduce(:merge)
         
     | 
| 
       21 
     | 
    
         
            -
                      through_records_by_owner = through_preloaders.map(&:records_by_owner).reduce(:merge)
         
     | 
| 
       22 
13 
     | 
    
         | 
| 
       23 
14 
     | 
    
         
             
                      @records_by_owner = owners.each_with_object({}) do |owner, result|
         
     | 
| 
      
 15 
     | 
    
         
            +
                        if loaded?(owner)
         
     | 
| 
      
 16 
     | 
    
         
            +
                          result[owner] = target_for(owner)
         
     | 
| 
      
 17 
     | 
    
         
            +
                          next
         
     | 
| 
      
 18 
     | 
    
         
            +
                        end
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
       24 
20 
     | 
    
         
             
                        through_records = through_records_by_owner[owner] || []
         
     | 
| 
       25 
21 
     | 
    
         | 
| 
       26 
     | 
    
         
            -
                        if  
     | 
| 
      
 22 
     | 
    
         
            +
                        if owners.first.association(through_reflection.name).loaded?
         
     | 
| 
       27 
23 
     | 
    
         
             
                          if source_type = reflection.options[:source_type]
         
     | 
| 
       28 
24 
     | 
    
         
             
                            through_records = through_records.select do |record|
         
     | 
| 
       29 
25 
     | 
    
         
             
                              record[reflection.foreign_type] == source_type
         
     | 
| 
         @@ -42,17 +38,47 @@ module ActiveRecord 
     | 
|
| 
       42 
38 
     | 
    
         
             
                      end
         
     | 
| 
       43 
39 
     | 
    
         
             
                    end
         
     | 
| 
       44 
40 
     | 
    
         | 
| 
      
 41 
     | 
    
         
            +
                    def runnable_loaders
         
     | 
| 
      
 42 
     | 
    
         
            +
                      if data_available?
         
     | 
| 
      
 43 
     | 
    
         
            +
                        [self]
         
     | 
| 
      
 44 
     | 
    
         
            +
                      elsif through_preloaders.all?(&:run?)
         
     | 
| 
      
 45 
     | 
    
         
            +
                        source_preloaders.flat_map(&:runnable_loaders)
         
     | 
| 
      
 46 
     | 
    
         
            +
                      else
         
     | 
| 
      
 47 
     | 
    
         
            +
                        through_preloaders.flat_map(&:runnable_loaders)
         
     | 
| 
      
 48 
     | 
    
         
            +
                      end
         
     | 
| 
      
 49 
     | 
    
         
            +
                    end
         
     | 
| 
      
 50 
     | 
    
         
            +
             
     | 
| 
      
 51 
     | 
    
         
            +
                    def future_classes
         
     | 
| 
      
 52 
     | 
    
         
            +
                      if run?
         
     | 
| 
      
 53 
     | 
    
         
            +
                        []
         
     | 
| 
      
 54 
     | 
    
         
            +
                      elsif through_preloaders.all?(&:run?)
         
     | 
| 
      
 55 
     | 
    
         
            +
                        source_preloaders.flat_map(&:future_classes).uniq
         
     | 
| 
      
 56 
     | 
    
         
            +
                      else
         
     | 
| 
      
 57 
     | 
    
         
            +
                        through_classes = through_preloaders.flat_map(&:future_classes)
         
     | 
| 
      
 58 
     | 
    
         
            +
                        source_classes = source_reflection.
         
     | 
| 
      
 59 
     | 
    
         
            +
                          chain.
         
     | 
| 
      
 60 
     | 
    
         
            +
                          reject { |reflection| reflection.respond_to?(:polymorphic?) && reflection.polymorphic? }.
         
     | 
| 
      
 61 
     | 
    
         
            +
                          map(&:klass)
         
     | 
| 
      
 62 
     | 
    
         
            +
                        (through_classes + source_classes).uniq
         
     | 
| 
      
 63 
     | 
    
         
            +
                      end
         
     | 
| 
      
 64 
     | 
    
         
            +
                    end
         
     | 
| 
      
 65 
     | 
    
         
            +
             
     | 
| 
       45 
66 
     | 
    
         
             
                    private
         
     | 
| 
      
 67 
     | 
    
         
            +
                      def data_available?
         
     | 
| 
      
 68 
     | 
    
         
            +
                        owners.all? { |owner| loaded?(owner) } ||
         
     | 
| 
      
 69 
     | 
    
         
            +
                          through_preloaders.all?(&:run?) && source_preloaders.all?(&:run?)
         
     | 
| 
      
 70 
     | 
    
         
            +
                      end
         
     | 
| 
      
 71 
     | 
    
         
            +
             
     | 
| 
       46 
72 
     | 
    
         
             
                      def source_preloaders
         
     | 
| 
       47 
     | 
    
         
            -
                        @source_preloaders ||=  
     | 
| 
      
 73 
     | 
    
         
            +
                        @source_preloaders ||= ActiveRecord::Associations::Preloader.new(records: middle_records, associations: source_reflection.name, scope: scope, associate_by_default: false).loaders
         
     | 
| 
       48 
74 
     | 
    
         
             
                      end
         
     | 
| 
       49 
75 
     | 
    
         | 
| 
       50 
76 
     | 
    
         
             
                      def middle_records
         
     | 
| 
       51 
     | 
    
         
            -
                         
     | 
| 
      
 77 
     | 
    
         
            +
                        through_records_by_owner.values.flatten
         
     | 
| 
       52 
78 
     | 
    
         
             
                      end
         
     | 
| 
       53 
79 
     | 
    
         | 
| 
       54 
80 
     | 
    
         
             
                      def through_preloaders
         
     | 
| 
       55 
     | 
    
         
            -
                        @through_preloaders ||=  
     | 
| 
      
 81 
     | 
    
         
            +
                        @through_preloaders ||= ActiveRecord::Associations::Preloader.new(records: owners, associations: through_reflection.name, scope: through_scope, associate_by_default: false).loaders
         
     | 
| 
       56 
82 
     | 
    
         
             
                      end
         
     | 
| 
       57 
83 
     | 
    
         | 
| 
       58 
84 
     | 
    
         
             
                      def through_reflection
         
     | 
| 
         @@ -63,6 +89,14 @@ module ActiveRecord 
     | 
|
| 
       63 
89 
     | 
    
         
             
                        reflection.source_reflection
         
     | 
| 
       64 
90 
     | 
    
         
             
                      end
         
     | 
| 
       65 
91 
     | 
    
         | 
| 
      
 92 
     | 
    
         
            +
                      def source_records_by_owner
         
     | 
| 
      
 93 
     | 
    
         
            +
                        @source_records_by_owner ||= source_preloaders.map(&:records_by_owner).reduce(:merge)
         
     | 
| 
      
 94 
     | 
    
         
            +
                      end
         
     | 
| 
      
 95 
     | 
    
         
            +
             
     | 
| 
      
 96 
     | 
    
         
            +
                      def through_records_by_owner
         
     | 
| 
      
 97 
     | 
    
         
            +
                        @through_records_by_owner ||= through_preloaders.map(&:records_by_owner).reduce(:merge)
         
     | 
| 
      
 98 
     | 
    
         
            +
                      end
         
     | 
| 
      
 99 
     | 
    
         
            +
             
     | 
| 
       66 
100 
     | 
    
         
             
                      def preload_index
         
     | 
| 
       67 
101 
     | 
    
         
             
                        @preload_index ||= preloaded_records.each_with_object({}).with_index do |(record, result), index|
         
     | 
| 
       68 
102 
     | 
    
         
             
                          result[record] = index
         
     | 
| 
         @@ -73,6 +107,8 @@ module ActiveRecord 
     | 
|
| 
       73 
107 
     | 
    
         
             
                        scope = through_reflection.klass.unscoped
         
     | 
| 
       74 
108 
     | 
    
         
             
                        options = reflection.options
         
     | 
| 
       75 
109 
     | 
    
         | 
| 
      
 110 
     | 
    
         
            +
                        return scope if options[:disable_joins]
         
     | 
| 
      
 111 
     | 
    
         
            +
             
     | 
| 
       76 
112 
     | 
    
         
             
                        values = reflection_scope.values
         
     | 
| 
       77 
113 
     | 
    
         
             
                        if annotations = values[:annotate]
         
     | 
| 
       78 
114 
     | 
    
         
             
                          scope.annotate!(*annotations)
         
     | 
| 
         @@ -108,7 +144,7 @@ module ActiveRecord 
     | 
|
| 
       108 
144 
     | 
    
         
             
                          end
         
     | 
| 
       109 
145 
     | 
    
         
             
                        end
         
     | 
| 
       110 
146 
     | 
    
         | 
| 
       111 
     | 
    
         
            -
                        scope
         
     | 
| 
      
 147 
     | 
    
         
            +
                        cascade_strict_loading(scope)
         
     | 
| 
       112 
148 
     | 
    
         
             
                      end
         
     | 
| 
       113 
149 
     | 
    
         
             
                  end
         
     | 
| 
       114 
150 
     | 
    
         
             
                end
         
     | 
| 
         @@ -4,6 +4,8 @@ require "active_support/core_ext/enumerable" 
     | 
|
| 
       4 
4 
     | 
    
         | 
| 
       5 
5 
     | 
    
         
             
            module ActiveRecord
         
     | 
| 
       6 
6 
     | 
    
         
             
              module Associations
         
     | 
| 
      
 7 
     | 
    
         
            +
                # = Active Record \Preloader
         
     | 
| 
      
 8 
     | 
    
         
            +
                #
         
     | 
| 
       7 
9 
     | 
    
         
             
                # Implements the details of eager loading of Active Record associations.
         
     | 
| 
       8 
10 
     | 
    
         
             
                #
         
     | 
| 
       9 
11 
     | 
    
         
             
                # Suppose that you have the following two Active Record models:
         
     | 
| 
         @@ -22,8 +24,8 @@ module ActiveRecord 
     | 
|
| 
       22 
24 
     | 
    
         
             
                #
         
     | 
| 
       23 
25 
     | 
    
         
             
                #   Author.includes(:books).where(name: ['bell hooks', 'Homer']).to_a
         
     | 
| 
       24 
26 
     | 
    
         
             
                #
         
     | 
| 
       25 
     | 
    
         
            -
                #    
     | 
| 
       26 
     | 
    
         
            -
                #    
     | 
| 
      
 27 
     | 
    
         
            +
                #   # SELECT `authors`.* FROM `authors` WHERE `name` IN ('bell hooks', 'Homer')
         
     | 
| 
      
 28 
     | 
    
         
            +
                #   # SELECT `books`.* FROM `books` WHERE `author_id` IN (2, 5)
         
     | 
| 
       27 
29 
     | 
    
         
             
                #
         
     | 
| 
       28 
30 
     | 
    
         
             
                # Active Record saves the ids of the records from the first query to use in
         
     | 
| 
       29 
31 
     | 
    
         
             
                # the second. Depending on the number of associations involved there can be
         
     | 
| 
         @@ -33,23 +35,26 @@ module ActiveRecord 
     | 
|
| 
       33 
35 
     | 
    
         
             
                # Record will fall back to a slightly more resource-intensive single query:
         
     | 
| 
       34 
36 
     | 
    
         
             
                #
         
     | 
| 
       35 
37 
     | 
    
         
             
                #   Author.includes(:books).where(books: {title: 'Illiad'}).to_a
         
     | 
| 
       36 
     | 
    
         
            -
                #    
     | 
| 
       37 
     | 
    
         
            -
                # 
     | 
| 
       38 
     | 
    
         
            -
                # 
     | 
| 
       39 
     | 
    
         
            -
                # 
     | 
| 
       40 
     | 
    
         
            -
                # 
     | 
| 
      
 38 
     | 
    
         
            +
                #   # SELECT `authors`.`id` AS t0_r0, `authors`.`name` AS t0_r1, `authors`.`age` AS t0_r2,
         
     | 
| 
      
 39 
     | 
    
         
            +
                #   #        `books`.`id`   AS t1_r0, `books`.`title`  AS t1_r1, `books`.`sales` AS t1_r2
         
     | 
| 
      
 40 
     | 
    
         
            +
                #   # FROM `authors`
         
     | 
| 
      
 41 
     | 
    
         
            +
                #   # LEFT OUTER JOIN `books` ON `authors`.`id` =  `books`.`author_id`
         
     | 
| 
      
 42 
     | 
    
         
            +
                #   # WHERE `books`.`title` = 'Illiad'
         
     | 
| 
       41 
43 
     | 
    
         
             
                #
         
     | 
| 
       42 
44 
     | 
    
         
             
                # This could result in many rows that contain redundant data and it performs poorly at scale
         
     | 
| 
       43 
45 
     | 
    
         
             
                # and is therefore only used when necessary.
         
     | 
| 
       44 
     | 
    
         
            -
                #
         
     | 
| 
       45 
     | 
    
         
            -
                class Preloader #:nodoc:
         
     | 
| 
      
 46 
     | 
    
         
            +
                class Preloader # :nodoc:
         
     | 
| 
       46 
47 
     | 
    
         
             
                  extend ActiveSupport::Autoload
         
     | 
| 
       47 
48 
     | 
    
         | 
| 
       48 
49 
     | 
    
         
             
                  eager_autoload do
         
     | 
| 
       49 
50 
     | 
    
         
             
                    autoload :Association,        "active_record/associations/preloader/association"
         
     | 
| 
      
 51 
     | 
    
         
            +
                    autoload :Batch,              "active_record/associations/preloader/batch"
         
     | 
| 
      
 52 
     | 
    
         
            +
                    autoload :Branch,             "active_record/associations/preloader/branch"
         
     | 
| 
       50 
53 
     | 
    
         
             
                    autoload :ThroughAssociation, "active_record/associations/preloader/through_association"
         
     | 
| 
       51 
54 
     | 
    
         
             
                  end
         
     | 
| 
       52 
55 
     | 
    
         | 
| 
      
 56 
     | 
    
         
            +
                  attr_reader :records, :associations, :scope, :associate_by_default
         
     | 
| 
      
 57 
     | 
    
         
            +
             
     | 
| 
       53 
58 
     | 
    
         
             
                  # Eager loads the named associations for the given Active Record record(s).
         
     | 
| 
       54 
59 
     | 
    
         
             
                  #
         
     | 
| 
       55 
60 
     | 
    
         
             
                  # In this description, 'association name' shall refer to the name passed
         
     | 
| 
         @@ -70,137 +75,61 @@ module ActiveRecord 
     | 
|
| 
       70 
75 
     | 
    
         
             
                  #   for an Author.
         
     | 
| 
       71 
76 
     | 
    
         
             
                  # - an Array which specifies multiple association names. This array
         
     | 
| 
       72 
77 
     | 
    
         
             
                  #   is processed recursively. For example, specifying <tt>[:avatar, :books]</tt>
         
     | 
| 
       73 
     | 
    
         
            -
                  #   allows this method to preload an author's avatar as well as all of  
     | 
| 
      
 78 
     | 
    
         
            +
                  #   allows this method to preload an author's avatar as well as all of their
         
     | 
| 
       74 
79 
     | 
    
         
             
                  #   books.
         
     | 
| 
       75 
80 
     | 
    
         
             
                  # - a Hash which specifies multiple association names, as well as
         
     | 
| 
       76 
81 
     | 
    
         
             
                  #   association names for the to-be-preloaded association objects. For
         
     | 
| 
       77 
82 
     | 
    
         
             
                  #   example, specifying <tt>{ author: :avatar }</tt> will preload a
         
     | 
| 
       78 
83 
     | 
    
         
             
                  #   book's author, as well as that author's avatar.
         
     | 
| 
       79 
84 
     | 
    
         
             
                  #
         
     | 
| 
       80 
     | 
    
         
            -
                  # +:associations+ has the same format as the  
     | 
| 
       81 
     | 
    
         
            -
                  #  
     | 
| 
      
 85 
     | 
    
         
            +
                  # +:associations+ has the same format as the arguments to
         
     | 
| 
      
 86 
     | 
    
         
            +
                  # ActiveRecord::QueryMethods#includes. So +associations+ could look like
         
     | 
| 
      
 87 
     | 
    
         
            +
                  # this:
         
     | 
| 
       82 
88 
     | 
    
         
             
                  #
         
     | 
| 
       83 
89 
     | 
    
         
             
                  #   :books
         
     | 
| 
       84 
90 
     | 
    
         
             
                  #   [ :books, :author ]
         
     | 
| 
       85 
91 
     | 
    
         
             
                  #   { author: :avatar }
         
     | 
| 
       86 
92 
     | 
    
         
             
                  #   [ :books, { author: :avatar } ]
         
     | 
| 
       87 
     | 
    
         
            -
                   
     | 
| 
       88 
     | 
    
         
            -
             
     | 
| 
      
 93 
     | 
    
         
            +
                  #
         
     | 
| 
      
 94 
     | 
    
         
            +
                  # +available_records+ is an array of ActiveRecord::Base. The Preloader
         
     | 
| 
      
 95 
     | 
    
         
            +
                  # will try to use the objects in this array to preload the requested
         
     | 
| 
      
 96 
     | 
    
         
            +
                  # associations before querying the database. This can save database
         
     | 
| 
      
 97 
     | 
    
         
            +
                  # queries by reusing in-memory objects. The optimization is only applied
         
     | 
| 
      
 98 
     | 
    
         
            +
                  # to single associations (i.e. :belongs_to, :has_one) with no scopes.
         
     | 
| 
      
 99 
     | 
    
         
            +
                  def initialize(records:, associations:, scope: nil, available_records: [], associate_by_default: true)
         
     | 
| 
      
 100 
     | 
    
         
            +
                    @records = records
         
     | 
| 
      
 101 
     | 
    
         
            +
                    @associations = associations
         
     | 
| 
      
 102 
     | 
    
         
            +
                    @scope = scope
         
     | 
| 
      
 103 
     | 
    
         
            +
                    @available_records = available_records || []
         
     | 
| 
      
 104 
     | 
    
         
            +
                    @associate_by_default = associate_by_default
         
     | 
| 
       89 
105 
     | 
    
         | 
| 
       90 
     | 
    
         
            -
                     
     | 
| 
       91 
     | 
    
         
            -
                       
     | 
| 
       92 
     | 
    
         
            -
             
     | 
| 
       93 
     | 
    
         
            -
                       
     | 
| 
       94 
     | 
    
         
            -
             
     | 
| 
       95 
     | 
    
         
            -
                       
     | 
| 
       96 
     | 
    
         
            -
                     
     | 
| 
      
 106 
     | 
    
         
            +
                    @tree = Branch.new(
         
     | 
| 
      
 107 
     | 
    
         
            +
                      parent: nil,
         
     | 
| 
      
 108 
     | 
    
         
            +
                      association: nil,
         
     | 
| 
      
 109 
     | 
    
         
            +
                      children: @associations,
         
     | 
| 
      
 110 
     | 
    
         
            +
                      associate_by_default: @associate_by_default,
         
     | 
| 
      
 111 
     | 
    
         
            +
                      scope: @scope
         
     | 
| 
      
 112 
     | 
    
         
            +
                    )
         
     | 
| 
      
 113 
     | 
    
         
            +
                    @tree.preloaded_records = @records
         
     | 
| 
       97 
114 
     | 
    
         
             
                  end
         
     | 
| 
       98 
115 
     | 
    
         | 
| 
       99 
     | 
    
         
            -
                  def  
     | 
| 
       100 
     | 
    
         
            -
                     
     | 
| 
      
 116 
     | 
    
         
            +
                  def empty?
         
     | 
| 
      
 117 
     | 
    
         
            +
                    associations.nil? || records.length == 0
         
     | 
| 
       101 
118 
     | 
    
         
             
                  end
         
     | 
| 
       102 
119 
     | 
    
         | 
| 
       103 
     | 
    
         
            -
                   
     | 
| 
       104 
     | 
    
         
            -
                     
     | 
| 
       105 
     | 
    
         
            -
                    def preloaders_on(association, records, scope, polymorphic_parent = false)
         
     | 
| 
       106 
     | 
    
         
            -
                      case association
         
     | 
| 
       107 
     | 
    
         
            -
                      when Hash
         
     | 
| 
       108 
     | 
    
         
            -
                        preloaders_for_hash(association, records, scope, polymorphic_parent)
         
     | 
| 
       109 
     | 
    
         
            -
                      when Symbol, String
         
     | 
| 
       110 
     | 
    
         
            -
                        preloaders_for_one(association, records, scope, polymorphic_parent)
         
     | 
| 
       111 
     | 
    
         
            -
                      else
         
     | 
| 
       112 
     | 
    
         
            -
                        raise ArgumentError, "#{association.inspect} was not recognized for preload"
         
     | 
| 
       113 
     | 
    
         
            -
                      end
         
     | 
| 
       114 
     | 
    
         
            -
                    end
         
     | 
| 
       115 
     | 
    
         
            -
             
     | 
| 
       116 
     | 
    
         
            -
                    def preloaders_for_hash(association, records, scope, polymorphic_parent)
         
     | 
| 
       117 
     | 
    
         
            -
                      association.flat_map { |parent, child|
         
     | 
| 
       118 
     | 
    
         
            -
                        grouped_records(parent, records, polymorphic_parent).flat_map do |reflection, reflection_records|
         
     | 
| 
       119 
     | 
    
         
            -
                          loaders = preloaders_for_reflection(reflection, reflection_records, scope)
         
     | 
| 
       120 
     | 
    
         
            -
                          recs = loaders.flat_map(&:preloaded_records).uniq
         
     | 
| 
       121 
     | 
    
         
            -
                          child_polymorphic_parent = reflection && reflection.options[:polymorphic]
         
     | 
| 
       122 
     | 
    
         
            -
                          loaders.concat Array.wrap(child).flat_map { |assoc|
         
     | 
| 
       123 
     | 
    
         
            -
                            preloaders_on assoc, recs, scope, child_polymorphic_parent
         
     | 
| 
       124 
     | 
    
         
            -
                          }
         
     | 
| 
       125 
     | 
    
         
            -
                          loaders
         
     | 
| 
       126 
     | 
    
         
            -
                        end
         
     | 
| 
       127 
     | 
    
         
            -
                      }
         
     | 
| 
       128 
     | 
    
         
            -
                    end
         
     | 
| 
       129 
     | 
    
         
            -
             
     | 
| 
       130 
     | 
    
         
            -
                    # Loads all the given data into +records+ for a singular +association+.
         
     | 
| 
       131 
     | 
    
         
            -
                    #
         
     | 
| 
       132 
     | 
    
         
            -
                    # Functions by instantiating a preloader class such as Preloader::Association and
         
     | 
| 
       133 
     | 
    
         
            -
                    # call the +run+ method for each passed in class in the +records+ argument.
         
     | 
| 
       134 
     | 
    
         
            -
                    #
         
     | 
| 
       135 
     | 
    
         
            -
                    # Not all records have the same class, so group then preload group on the reflection
         
     | 
| 
       136 
     | 
    
         
            -
                    # itself so that if various subclass share the same association then we do not split
         
     | 
| 
       137 
     | 
    
         
            -
                    # them unnecessarily
         
     | 
| 
       138 
     | 
    
         
            -
                    #
         
     | 
| 
       139 
     | 
    
         
            -
                    # Additionally, polymorphic belongs_to associations can have multiple associated
         
     | 
| 
       140 
     | 
    
         
            -
                    # classes, depending on the polymorphic_type field. So we group by the classes as
         
     | 
| 
       141 
     | 
    
         
            -
                    # well.
         
     | 
| 
       142 
     | 
    
         
            -
                    def preloaders_for_one(association, records, scope, polymorphic_parent)
         
     | 
| 
       143 
     | 
    
         
            -
                      grouped_records(association, records, polymorphic_parent)
         
     | 
| 
       144 
     | 
    
         
            -
                        .flat_map do |reflection, reflection_records|
         
     | 
| 
       145 
     | 
    
         
            -
                          preloaders_for_reflection reflection, reflection_records, scope
         
     | 
| 
       146 
     | 
    
         
            -
                        end
         
     | 
| 
       147 
     | 
    
         
            -
                    end
         
     | 
| 
       148 
     | 
    
         
            -
             
     | 
| 
       149 
     | 
    
         
            -
                    def preloaders_for_reflection(reflection, records, scope)
         
     | 
| 
       150 
     | 
    
         
            -
                      records.group_by { |record| record.association(reflection.name).klass }.map do |rhs_klass, rs|
         
     | 
| 
       151 
     | 
    
         
            -
                        preloader_for(reflection, rs).new(rhs_klass, rs, reflection, scope, @associate_by_default).run
         
     | 
| 
       152 
     | 
    
         
            -
                      end
         
     | 
| 
       153 
     | 
    
         
            -
                    end
         
     | 
| 
      
 120 
     | 
    
         
            +
                  def call
         
     | 
| 
      
 121 
     | 
    
         
            +
                    Batch.new([self], available_records: @available_records).call
         
     | 
| 
       154 
122 
     | 
    
         | 
| 
       155 
     | 
    
         
            -
                     
     | 
| 
       156 
     | 
    
         
            -
             
     | 
| 
       157 
     | 
    
         
            -
                      records.each do |record|
         
     | 
| 
       158 
     | 
    
         
            -
                        reflection = record.class._reflect_on_association(association)
         
     | 
| 
       159 
     | 
    
         
            -
                        next if polymorphic_parent && !reflection || !record.association(association).klass
         
     | 
| 
       160 
     | 
    
         
            -
                        (h[reflection] ||= []) << record
         
     | 
| 
       161 
     | 
    
         
            -
                      end
         
     | 
| 
       162 
     | 
    
         
            -
                      h
         
     | 
| 
       163 
     | 
    
         
            -
                    end
         
     | 
| 
       164 
     | 
    
         
            -
             
     | 
| 
       165 
     | 
    
         
            -
                    class AlreadyLoaded # :nodoc:
         
     | 
| 
       166 
     | 
    
         
            -
                      def initialize(klass, owners, reflection, preload_scope, associate_by_default = true)
         
     | 
| 
       167 
     | 
    
         
            -
                        @owners = owners
         
     | 
| 
       168 
     | 
    
         
            -
                        @reflection = reflection
         
     | 
| 
       169 
     | 
    
         
            -
                      end
         
     | 
| 
       170 
     | 
    
         
            -
             
     | 
| 
       171 
     | 
    
         
            -
                      def run
         
     | 
| 
       172 
     | 
    
         
            -
                        self
         
     | 
| 
       173 
     | 
    
         
            -
                      end
         
     | 
| 
       174 
     | 
    
         
            -
             
     | 
| 
       175 
     | 
    
         
            -
                      def preloaded_records
         
     | 
| 
       176 
     | 
    
         
            -
                        @preloaded_records ||= records_by_owner.flat_map(&:last)
         
     | 
| 
       177 
     | 
    
         
            -
                      end
         
     | 
| 
       178 
     | 
    
         
            -
             
     | 
| 
       179 
     | 
    
         
            -
                      def records_by_owner
         
     | 
| 
       180 
     | 
    
         
            -
                        @records_by_owner ||= owners.index_with do |owner|
         
     | 
| 
       181 
     | 
    
         
            -
                          Array(owner.association(reflection.name).target)
         
     | 
| 
       182 
     | 
    
         
            -
                        end
         
     | 
| 
       183 
     | 
    
         
            -
                      end
         
     | 
| 
       184 
     | 
    
         
            -
             
     | 
| 
       185 
     | 
    
         
            -
                      private
         
     | 
| 
       186 
     | 
    
         
            -
                        attr_reader :owners, :reflection
         
     | 
| 
       187 
     | 
    
         
            -
                    end
         
     | 
| 
      
 123 
     | 
    
         
            +
                    loaders
         
     | 
| 
      
 124 
     | 
    
         
            +
                  end
         
     | 
| 
       188 
125 
     | 
    
         | 
| 
       189 
     | 
    
         
            -
             
     | 
| 
       190 
     | 
    
         
            -
                     
     | 
| 
       191 
     | 
    
         
            -
             
     | 
| 
       192 
     | 
    
         
            -
                    def preloader_for(reflection, owners)
         
     | 
| 
       193 
     | 
    
         
            -
                      if owners.all? { |o| o.association(reflection.name).loaded? }
         
     | 
| 
       194 
     | 
    
         
            -
                        return AlreadyLoaded
         
     | 
| 
       195 
     | 
    
         
            -
                      end
         
     | 
| 
       196 
     | 
    
         
            -
                      reflection.check_preloadable!
         
     | 
| 
      
 126 
     | 
    
         
            +
                  def branches
         
     | 
| 
      
 127 
     | 
    
         
            +
                    @tree.children
         
     | 
| 
      
 128 
     | 
    
         
            +
                  end
         
     | 
| 
       197 
129 
     | 
    
         | 
| 
       198 
     | 
    
         
            -
             
     | 
| 
       199 
     | 
    
         
            -
             
     | 
| 
       200 
     | 
    
         
            -
             
     | 
| 
       201 
     | 
    
         
            -
                        Association
         
     | 
| 
       202 
     | 
    
         
            -
                      end
         
     | 
| 
       203 
     | 
    
         
            -
                    end
         
     | 
| 
      
 130 
     | 
    
         
            +
                  def loaders
         
     | 
| 
      
 131 
     | 
    
         
            +
                    branches.flat_map(&:loaders)
         
     | 
| 
      
 132 
     | 
    
         
            +
                  end
         
     | 
| 
       204 
133 
     | 
    
         
             
                end
         
     | 
| 
       205 
134 
     | 
    
         
             
              end
         
     | 
| 
       206 
135 
     | 
    
         
             
            end
         
     | 
| 
         @@ -2,9 +2,11 @@ 
     | 
|
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            module ActiveRecord
         
     | 
| 
       4 
4 
     | 
    
         
             
              module Associations
         
     | 
| 
       5 
     | 
    
         
            -
                class SingularAssociation < Association  
     | 
| 
      
 5 
     | 
    
         
            +
                class SingularAssociation < Association # :nodoc:
         
     | 
| 
       6 
6 
     | 
    
         
             
                  # Implements the reader method, e.g. foo.bar for Foo.has_one :bar
         
     | 
| 
       7 
7 
     | 
    
         
             
                  def reader
         
     | 
| 
      
 8 
     | 
    
         
            +
                    ensure_klass_exists!
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
       8 
10 
     | 
    
         
             
                    if !loaded? || stale_target?
         
     | 
| 
       9 
11 
     | 
    
         
             
                      reload
         
     | 
| 
       10 
12 
     | 
    
         
             
                    end
         
     | 
| 
         @@ -32,11 +34,15 @@ module ActiveRecord 
     | 
|
| 
       32 
34 
     | 
    
         | 
| 
       33 
35 
     | 
    
         
             
                  private
         
     | 
| 
       34 
36 
     | 
    
         
             
                    def scope_for_create
         
     | 
| 
       35 
     | 
    
         
            -
                      super.except!(klass.primary_key)
         
     | 
| 
      
 37 
     | 
    
         
            +
                      super.except!(*Array(klass.primary_key))
         
     | 
| 
       36 
38 
     | 
    
         
             
                    end
         
     | 
| 
       37 
39 
     | 
    
         | 
| 
       38 
40 
     | 
    
         
             
                    def find_target
         
     | 
| 
       39 
     | 
    
         
            -
                       
     | 
| 
      
 41 
     | 
    
         
            +
                      if disable_joins
         
     | 
| 
      
 42 
     | 
    
         
            +
                        scope.first
         
     | 
| 
      
 43 
     | 
    
         
            +
                      else
         
     | 
| 
      
 44 
     | 
    
         
            +
                        super.first
         
     | 
| 
      
 45 
     | 
    
         
            +
                      end
         
     | 
| 
       40 
46 
     | 
    
         
             
                    end
         
     | 
| 
       41 
47 
     | 
    
         | 
| 
       42 
48 
     | 
    
         
             
                    def replace(record)
         
     | 
| 
         @@ -3,10 +3,14 @@ 
     | 
|
| 
       3 
3 
     | 
    
         
             
            module ActiveRecord
         
     | 
| 
       4 
4 
     | 
    
         
             
              module Associations
         
     | 
| 
       5 
5 
     | 
    
         
             
                # = Active Record Through Association
         
     | 
| 
       6 
     | 
    
         
            -
                module ThroughAssociation  
     | 
| 
      
 6 
     | 
    
         
            +
                module ThroughAssociation # :nodoc:
         
     | 
| 
       7 
7 
     | 
    
         
             
                  delegate :source_reflection, to: :reflection
         
     | 
| 
       8 
8 
     | 
    
         | 
| 
       9 
9 
     | 
    
         
             
                  private
         
     | 
| 
      
 10 
     | 
    
         
            +
                    def transaction(&block)
         
     | 
| 
      
 11 
     | 
    
         
            +
                      through_reflection.klass.transaction(&block)
         
     | 
| 
      
 12 
     | 
    
         
            +
                    end
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
       10 
14 
     | 
    
         
             
                    def through_reflection
         
     | 
| 
       11 
15 
     | 
    
         
             
                      @through_reflection ||= begin
         
     | 
| 
       12 
16 
     | 
    
         
             
                        refl = reflection.through_reflection
         
     | 
| 
         @@ -55,12 +59,11 @@ module ActiveRecord 
     | 
|
| 
       55 
59 
     | 
    
         | 
| 
       56 
60 
     | 
    
         
             
                      association_primary_key = source_reflection.association_primary_key(reflection.klass)
         
     | 
| 
       57 
61 
     | 
    
         | 
| 
       58 
     | 
    
         
            -
                      if association_primary_key == reflection.klass. 
     | 
| 
      
 62 
     | 
    
         
            +
                      if Array(association_primary_key) == reflection.klass.composite_query_constraints_list && !options[:source_type]
         
     | 
| 
       59 
63 
     | 
    
         
             
                        join_attributes = { source_reflection.name => records }
         
     | 
| 
       60 
64 
     | 
    
         
             
                      else
         
     | 
| 
       61 
     | 
    
         
            -
                         
     | 
| 
       62 
     | 
    
         
            -
             
     | 
| 
       63 
     | 
    
         
            -
                        }
         
     | 
| 
      
 65 
     | 
    
         
            +
                        assoc_pk_values = records.map { |record| record._read_attribute(association_primary_key) }
         
     | 
| 
      
 66 
     | 
    
         
            +
                        join_attributes = { source_reflection.foreign_key => assoc_pk_values }
         
     | 
| 
       64 
67 
     | 
    
         
             
                      end
         
     | 
| 
       65 
68 
     | 
    
         | 
| 
       66 
69 
     | 
    
         
             
                      if options[:source_type]
         
     | 
| 
         @@ -74,16 +77,20 @@ module ActiveRecord 
     | 
|
| 
       74 
77 
     | 
    
         
             
                      end
         
     | 
| 
       75 
78 
     | 
    
         
             
                    end
         
     | 
| 
       76 
79 
     | 
    
         | 
| 
       77 
     | 
    
         
            -
                    # Note: this does not capture all cases, for example it would be  
     | 
| 
       78 
     | 
    
         
            -
                    # properly support stale-checking for nested associations.
         
     | 
| 
      
 80 
     | 
    
         
            +
                    # Note: this does not capture all cases, for example it would be impractical
         
     | 
| 
      
 81 
     | 
    
         
            +
                    # to try to properly support stale-checking for nested associations.
         
     | 
| 
       79 
82 
     | 
    
         
             
                    def stale_state
         
     | 
| 
       80 
83 
     | 
    
         
             
                      if through_reflection.belongs_to?
         
     | 
| 
       81 
     | 
    
         
            -
                         
     | 
| 
      
 84 
     | 
    
         
            +
                        Array(through_reflection.foreign_key).filter_map do |foreign_key_column|
         
     | 
| 
      
 85 
     | 
    
         
            +
                          owner[foreign_key_column] && owner[foreign_key_column].to_s
         
     | 
| 
      
 86 
     | 
    
         
            +
                        end.presence
         
     | 
| 
       82 
87 
     | 
    
         
             
                      end
         
     | 
| 
       83 
88 
     | 
    
         
             
                    end
         
     | 
| 
       84 
89 
     | 
    
         | 
| 
       85 
90 
     | 
    
         
             
                    def foreign_key_present?
         
     | 
| 
       86 
     | 
    
         
            -
                      through_reflection.belongs_to? &&  
     | 
| 
      
 91 
     | 
    
         
            +
                      through_reflection.belongs_to? && Array(through_reflection.foreign_key).all? do |foreign_key_column|
         
     | 
| 
      
 92 
     | 
    
         
            +
                        !owner[foreign_key_column].nil?
         
     | 
| 
      
 93 
     | 
    
         
            +
                      end
         
     | 
| 
       87 
94 
     | 
    
         
             
                    end
         
     | 
| 
       88 
95 
     | 
    
         | 
| 
       89 
96 
     | 
    
         
             
                    def ensure_mutable
         
     | 
| 
         @@ -107,11 +114,15 @@ module ActiveRecord 
     | 
|
| 
       107 
114 
     | 
    
         
             
                    end
         
     | 
| 
       108 
115 
     | 
    
         | 
| 
       109 
116 
     | 
    
         
             
                    def build_record(attributes)
         
     | 
| 
       110 
     | 
    
         
            -
                       
     | 
| 
       111 
     | 
    
         
            -
             
     | 
| 
       112 
     | 
    
         
            -
             
     | 
| 
       113 
     | 
    
         
            -
             
     | 
| 
       114 
     | 
    
         
            -
                         
     | 
| 
      
 117 
     | 
    
         
            +
                      if source_reflection.collection?
         
     | 
| 
      
 118 
     | 
    
         
            +
                        inverse = source_reflection.inverse_of
         
     | 
| 
      
 119 
     | 
    
         
            +
                        target = through_association.target
         
     | 
| 
      
 120 
     | 
    
         
            +
             
     | 
| 
      
 121 
     | 
    
         
            +
                        if inverse && target && !target.is_a?(Array)
         
     | 
| 
      
 122 
     | 
    
         
            +
                          Array(target.id).zip(Array(inverse.foreign_key)).map do |primary_key_value, foreign_key_column|
         
     | 
| 
      
 123 
     | 
    
         
            +
                            attributes[foreign_key_column] = primary_key_value
         
     | 
| 
      
 124 
     | 
    
         
            +
                          end
         
     | 
| 
      
 125 
     | 
    
         
            +
                        end
         
     | 
| 
       115 
126 
     | 
    
         
             
                      end
         
     | 
| 
       116 
127 
     | 
    
         | 
| 
       117 
128 
     | 
    
         
             
                      super
         
     |