activerecord 7.1.0.beta1 → 7.1.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +70 -4
  3. data/lib/active_record/associations/collection_association.rb +1 -3
  4. data/lib/active_record/associations/collection_proxy.rb +1 -1
  5. data/lib/active_record/associations.rb +107 -107
  6. data/lib/active_record/connection_adapters/abstract/database_statements.rb +20 -1
  7. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +3 -3
  8. data/lib/active_record/connection_adapters/abstract/transaction.rb +12 -9
  9. data/lib/active_record/connection_adapters/abstract_adapter.rb +1 -1
  10. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +7 -0
  11. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +1 -1
  12. data/lib/active_record/connection_adapters/pool_manager.rb +1 -1
  13. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +0 -16
  14. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +5 -5
  15. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +24 -24
  16. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +19 -8
  17. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +25 -29
  18. data/lib/active_record/connection_adapters/postgresql_adapter.rb +1 -6
  19. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +4 -4
  20. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +49 -4
  21. data/lib/active_record/core.rb +7 -9
  22. data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +1 -1
  23. data/lib/active_record/errors.rb +19 -0
  24. data/lib/active_record/gem_version.rb +1 -1
  25. data/lib/active_record/migration/command_recorder.rb +8 -8
  26. data/lib/active_record/normalization.rb +2 -1
  27. data/lib/active_record/railtie.rb +1 -1
  28. data/lib/active_record/reflection.rb +10 -16
  29. data/lib/active_record/relation/finder_methods.rb +3 -12
  30. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +4 -6
  31. data/lib/active_record/schema_dumper.rb +9 -4
  32. metadata +9 -9
@@ -1276,15 +1276,15 @@ module ActiveRecord
1276
1276
  # +collection+ is a placeholder for the symbol passed as the +name+ argument, so
1277
1277
  # <tt>has_many :clients</tt> would add among others <tt>clients.empty?</tt>.
1278
1278
  #
1279
- # [collection]
1279
+ # [<tt>collection</tt>]
1280
1280
  # Returns a Relation of all the associated objects.
1281
1281
  # An empty Relation is returned if none are found.
1282
- # [collection<<(object, ...)]
1282
+ # [<tt>collection<<(object, ...)</tt>]
1283
1283
  # Adds one or more objects to the collection by setting their foreign keys to the collection's primary key.
1284
1284
  # Note that this operation instantly fires update SQL without waiting for the save or update call on the
1285
1285
  # parent object, unless the parent object is a new record.
1286
1286
  # This will also run validations and callbacks of associated object(s).
1287
- # [collection.delete(object, ...)]
1287
+ # [<tt>collection.delete(object, ...)</tt>]
1288
1288
  # Removes one or more objects from the collection by setting their foreign keys to +NULL+.
1289
1289
  # Objects will be in addition destroyed if they're associated with <tt>dependent: :destroy</tt>,
1290
1290
  # and deleted if they're associated with <tt>dependent: :delete_all</tt>.
@@ -1292,50 +1292,50 @@ module ActiveRecord
1292
1292
  # If the <tt>:through</tt> option is used, then the join records are deleted (rather than
1293
1293
  # nullified) by default, but you can specify <tt>dependent: :destroy</tt> or
1294
1294
  # <tt>dependent: :nullify</tt> to override this.
1295
- # [collection.destroy(object, ...)]
1295
+ # [<tt>collection.destroy(object, ...)</tt>]
1296
1296
  # Removes one or more objects from the collection by running <tt>destroy</tt> on
1297
1297
  # each record, regardless of any dependent option, ensuring callbacks are run.
1298
1298
  #
1299
1299
  # If the <tt>:through</tt> option is used, then the join records are destroyed
1300
1300
  # instead, not the objects themselves.
1301
- # [collection=objects]
1301
+ # [<tt>collection=objects</tt>]
1302
1302
  # Replaces the collections content by deleting and adding objects as appropriate. If the <tt>:through</tt>
1303
1303
  # option is true callbacks in the join models are triggered except destroy callbacks, since deletion is
1304
1304
  # direct by default. You can specify <tt>dependent: :destroy</tt> or
1305
1305
  # <tt>dependent: :nullify</tt> to override this.
1306
- # [collection_singular_ids]
1306
+ # [<tt>collection_singular_ids</tt>]
1307
1307
  # Returns an array of the associated objects' ids
1308
- # [collection_singular_ids=ids]
1308
+ # [<tt>collection_singular_ids=ids</tt>]
1309
1309
  # Replace the collection with the objects identified by the primary keys in +ids+. This
1310
1310
  # method loads the models and calls <tt>collection=</tt>. See above.
1311
- # [collection.clear]
1311
+ # [<tt>collection.clear</tt>]
1312
1312
  # Removes every object from the collection. This destroys the associated objects if they
1313
1313
  # are associated with <tt>dependent: :destroy</tt>, deletes them directly from the
1314
1314
  # database if <tt>dependent: :delete_all</tt>, otherwise sets their foreign keys to +NULL+.
1315
1315
  # If the <tt>:through</tt> option is true no destroy callbacks are invoked on the join models.
1316
1316
  # Join models are directly deleted.
1317
- # [collection.empty?]
1317
+ # [<tt>collection.empty?</tt>]
1318
1318
  # Returns +true+ if there are no associated objects.
1319
- # [collection.size]
1319
+ # [<tt>collection.size</tt>]
1320
1320
  # Returns the number of associated objects.
1321
- # [collection.find(...)]
1321
+ # [<tt>collection.find(...)</tt>]
1322
1322
  # Finds an associated object according to the same rules as ActiveRecord::FinderMethods#find.
1323
- # [collection.exists?(...)]
1323
+ # [<tt>collection.exists?(...)</tt>]
1324
1324
  # Checks whether an associated object with the given conditions exists.
1325
1325
  # Uses the same rules as ActiveRecord::FinderMethods#exists?.
1326
- # [collection.build(attributes = {}, ...)]
1326
+ # [<tt>collection.build(attributes = {}, ...)</tt>]
1327
1327
  # Returns one or more new objects of the collection type that have been instantiated
1328
1328
  # with +attributes+ and linked to this object through a foreign key, but have not yet
1329
1329
  # been saved.
1330
- # [collection.create(attributes = {})]
1330
+ # [<tt>collection.create(attributes = {})</tt>]
1331
1331
  # Returns a new object of the collection type that has been instantiated
1332
1332
  # with +attributes+, linked to this object through a foreign key, and that has already
1333
1333
  # been saved (if it passed the validation). *Note*: This only works if the base model
1334
1334
  # already exists in the DB, not if it is a new (unsaved) record!
1335
- # [collection.create!(attributes = {})]
1335
+ # [<tt>collection.create!(attributes = {})</tt>]
1336
1336
  # Does the same as <tt>collection.create</tt>, but raises ActiveRecord::RecordInvalid
1337
1337
  # if the record is invalid.
1338
- # [collection.reload]
1338
+ # [<tt>collection.reload</tt>]
1339
1339
  # Returns a Relation of all of the associated objects, forcing a database read.
1340
1340
  # An empty Relation is returned if none are found.
1341
1341
  #
@@ -1395,27 +1395,27 @@ module ActiveRecord
1395
1395
  # end
1396
1396
  #
1397
1397
  # === Options
1398
- # [:class_name]
1398
+ # [+:class_name+]
1399
1399
  # Specify the class name of the association. Use it only if that name can't be inferred
1400
1400
  # from the association name. So <tt>has_many :products</tt> will by default be linked
1401
1401
  # to the +Product+ class, but if the real class name is +SpecialProduct+, you'll have to
1402
1402
  # specify it with this option.
1403
- # [:foreign_key]
1403
+ # [+:foreign_key+]
1404
1404
  # Specify the foreign key used for the association. By default this is guessed to be the name
1405
1405
  # of this class in lower-case and "_id" suffixed. So a Person class that makes a #has_many
1406
1406
  # association will use "person_id" as the default <tt>:foreign_key</tt>.
1407
1407
  #
1408
1408
  # Setting the <tt>:foreign_key</tt> option prevents automatic detection of the association's
1409
1409
  # inverse, so it is generally a good idea to set the <tt>:inverse_of</tt> option as well.
1410
- # [:foreign_type]
1410
+ # [+:foreign_type+]
1411
1411
  # Specify the column used to store the associated object's type, if this is a polymorphic
1412
1412
  # association. By default this is guessed to be the name of the polymorphic association
1413
1413
  # specified on "as" option with a "_type" suffix. So a class that defines a
1414
1414
  # <tt>has_many :tags, as: :taggable</tt> association will use "taggable_type" as the
1415
1415
  # default <tt>:foreign_type</tt>.
1416
- # [:primary_key]
1416
+ # [+:primary_key+]
1417
1417
  # Specify the name of the column to use as the primary key for the association. By default this is +id+.
1418
- # [:dependent]
1418
+ # [+:dependent+]
1419
1419
  # Controls what happens to the associated objects when
1420
1420
  # their owner is destroyed. Note that these are implemented as
1421
1421
  # callbacks, and \Rails executes callbacks in order. Therefore, other
@@ -1442,12 +1442,12 @@ module ActiveRecord
1442
1442
  # <tt>has_many :comments, -> { where published: true }, dependent: :destroy</tt> and <tt>destroy</tt> is
1443
1443
  # called on a post, only published comments are destroyed. This means that any unpublished comments in the
1444
1444
  # database would still contain a foreign key pointing to the now deleted post.
1445
- # [:counter_cache]
1445
+ # [+:counter_cache+]
1446
1446
  # This option can be used to configure a custom named <tt>:counter_cache.</tt> You only need this option,
1447
1447
  # when you customized the name of your <tt>:counter_cache</tt> on the #belongs_to association.
1448
- # [:as]
1448
+ # [+:as+]
1449
1449
  # Specifies a polymorphic interface (See #belongs_to).
1450
- # [:through]
1450
+ # [+:through+]
1451
1451
  # Specifies an association through which to perform the query. This can be any other type
1452
1452
  # of association, including other <tt>:through</tt> associations. Options for <tt>:class_name</tt>,
1453
1453
  # <tt>:primary_key</tt> and <tt>:foreign_key</tt> are ignored, as the association uses the
@@ -1463,23 +1463,23 @@ module ActiveRecord
1463
1463
  # join model. This allows associated records to be built which will automatically create
1464
1464
  # the appropriate join model records when they are saved. (See the 'Association Join Models'
1465
1465
  # and 'Setting Inverses' sections above.)
1466
- # [:disable_joins]
1466
+ # [+:disable_joins+]
1467
1467
  # Specifies whether joins should be skipped for an association. If set to true, two or more queries
1468
1468
  # will be generated. Note that in some cases, if order or limit is applied, it will be done in-memory
1469
1469
  # due to database limitations. This option is only applicable on <tt>has_many :through</tt> associations as
1470
1470
  # +has_many+ alone do not perform a join.
1471
- # [:source]
1471
+ # [+:source+]
1472
1472
  # Specifies the source association name used by #has_many <tt>:through</tt> queries.
1473
1473
  # Only use it if the name cannot be inferred from the association.
1474
1474
  # <tt>has_many :subscribers, through: :subscriptions</tt> will look for either <tt>:subscribers</tt> or
1475
1475
  # <tt>:subscriber</tt> on Subscription, unless a <tt>:source</tt> is given.
1476
- # [:source_type]
1476
+ # [+:source_type+]
1477
1477
  # Specifies type of the source association used by #has_many <tt>:through</tt> queries where the source
1478
1478
  # association is a polymorphic #belongs_to.
1479
- # [:validate]
1479
+ # [+:validate+]
1480
1480
  # When set to +true+, validates new objects added to association when saving the parent object. +true+ by default.
1481
1481
  # If you want to ensure associated objects are revalidated on every update, use +validates_associated+.
1482
- # [:autosave]
1482
+ # [+:autosave+]
1483
1483
  # If true, always save the associated objects or destroy them if marked for destruction,
1484
1484
  # when saving the parent object. If false, never save or destroy the associated objects.
1485
1485
  # By default, only save associated objects that are new records. This option is implemented as a
@@ -1488,21 +1488,21 @@ module ActiveRecord
1488
1488
  #
1489
1489
  # Note that NestedAttributes::ClassMethods#accepts_nested_attributes_for sets
1490
1490
  # <tt>:autosave</tt> to <tt>true</tt>.
1491
- # [:inverse_of]
1491
+ # [+:inverse_of+]
1492
1492
  # Specifies the name of the #belongs_to association on the associated object
1493
1493
  # that is the inverse of this #has_many association.
1494
1494
  # See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail.
1495
- # [:extend]
1495
+ # [+:extend+]
1496
1496
  # Specifies a module or array of modules that will be extended into the association object returned.
1497
1497
  # Useful for defining methods on associations, especially when they should be shared between multiple
1498
1498
  # association objects.
1499
- # [:strict_loading]
1499
+ # [+:strict_loading+]
1500
1500
  # When set to +true+, enforces strict loading every time the associated record is loaded through this
1501
1501
  # association.
1502
- # [:ensuring_owner_was]
1502
+ # [+:ensuring_owner_was+]
1503
1503
  # Specifies an instance method to be called on the owner. The method must return true in order for the
1504
1504
  # associated records to be deleted in a background job.
1505
- # [:query_constraints]
1505
+ # [+:query_constraints+]
1506
1506
  # Serves as a composite foreign key. Defines the list of columns to be used to query the associated object.
1507
1507
  # This is an optional option. By default Rails will attempt to derive the value automatically.
1508
1508
  # When the value is set the Array size must match associated model's primary key or `query_constraints` size.
@@ -1534,26 +1534,26 @@ module ActiveRecord
1534
1534
  # +association+ is a placeholder for the symbol passed as the +name+ argument, so
1535
1535
  # <tt>has_one :manager</tt> would add among others <tt>manager.nil?</tt>.
1536
1536
  #
1537
- # [association]
1537
+ # [<tt>association</tt>]
1538
1538
  # Returns the associated object. +nil+ is returned if none is found.
1539
- # [association=(associate)]
1539
+ # [<tt>association=(associate)</tt>]
1540
1540
  # Assigns the associate object, extracts the primary key, sets it as the foreign key,
1541
1541
  # and saves the associate object. To avoid database inconsistencies, permanently deletes an existing
1542
1542
  # associated object when assigning a new one, even if the new one isn't saved to database.
1543
- # [build_association(attributes = {})]
1543
+ # [<tt>build_association(attributes = {})</tt>]
1544
1544
  # Returns a new object of the associated type that has been instantiated
1545
1545
  # with +attributes+ and linked to this object through a foreign key, but has not
1546
1546
  # yet been saved.
1547
- # [create_association(attributes = {})]
1547
+ # [<tt>create_association(attributes = {})</tt>]
1548
1548
  # Returns a new object of the associated type that has been instantiated
1549
1549
  # with +attributes+, linked to this object through a foreign key, and that
1550
1550
  # has already been saved (if it passed the validation).
1551
- # [create_association!(attributes = {})]
1551
+ # [<tt>create_association!(attributes = {})</tt>]
1552
1552
  # Does the same as <tt>create_association</tt>, but raises ActiveRecord::RecordInvalid
1553
1553
  # if the record is invalid.
1554
- # [reload_association]
1554
+ # [<tt>reload_association</tt>]
1555
1555
  # Returns the associated object, forcing a database read.
1556
- # [reset_association]
1556
+ # [<tt>reset_association</tt>]
1557
1557
  # Unloads the associated object. The next access will query it from the database.
1558
1558
  #
1559
1559
  # === Example
@@ -1591,11 +1591,11 @@ module ActiveRecord
1591
1591
  # The declaration can also include an +options+ hash to specialize the behavior of the association.
1592
1592
  #
1593
1593
  # Options are:
1594
- # [:class_name]
1594
+ # [+:class_name+]
1595
1595
  # Specify the class name of the association. Use it only if that name can't be inferred
1596
1596
  # from the association name. So <tt>has_one :manager</tt> will by default be linked to the Manager class, but
1597
1597
  # if the real class name is Person, you'll have to specify it with this option.
1598
- # [:dependent]
1598
+ # [+:dependent+]
1599
1599
  # Controls what happens to the associated object when
1600
1600
  # its owner is destroyed:
1601
1601
  #
@@ -1611,24 +1611,24 @@ module ActiveRecord
1611
1611
  # * <tt>:restrict_with_error</tt> causes an error to be added to the owner if there is an associated object
1612
1612
  #
1613
1613
  # Note that <tt>:dependent</tt> option is ignored when using <tt>:through</tt> option.
1614
- # [:foreign_key]
1614
+ # [+:foreign_key+]
1615
1615
  # Specify the foreign key used for the association. By default this is guessed to be the name
1616
1616
  # of this class in lower-case and "_id" suffixed. So a Person class that makes a #has_one association
1617
1617
  # will use "person_id" as the default <tt>:foreign_key</tt>.
1618
1618
  #
1619
1619
  # Setting the <tt>:foreign_key</tt> option prevents automatic detection of the association's
1620
1620
  # inverse, so it is generally a good idea to set the <tt>:inverse_of</tt> option as well.
1621
- # [:foreign_type]
1621
+ # [+:foreign_type+]
1622
1622
  # Specify the column used to store the associated object's type, if this is a polymorphic
1623
1623
  # association. By default this is guessed to be the name of the polymorphic association
1624
1624
  # specified on "as" option with a "_type" suffix. So a class that defines a
1625
1625
  # <tt>has_one :tag, as: :taggable</tt> association will use "taggable_type" as the
1626
1626
  # default <tt>:foreign_type</tt>.
1627
- # [:primary_key]
1627
+ # [+:primary_key+]
1628
1628
  # Specify the method that returns the primary key used for the association. By default this is +id+.
1629
- # [:as]
1629
+ # [+:as+]
1630
1630
  # Specifies a polymorphic interface (See #belongs_to).
1631
- # [:through]
1631
+ # [+:through+]
1632
1632
  # Specifies a Join Model through which to perform the query. Options for <tt>:class_name</tt>,
1633
1633
  # <tt>:primary_key</tt>, and <tt>:foreign_key</tt> are ignored, as the association uses the
1634
1634
  # source reflection. You can only use a <tt>:through</tt> query through a #has_one
@@ -1644,49 +1644,49 @@ module ActiveRecord
1644
1644
  # join model. This allows associated records to be built which will automatically create
1645
1645
  # the appropriate join model records when they are saved. (See the 'Association Join Models'
1646
1646
  # and 'Setting Inverses' sections above.)
1647
- # [:disable_joins]
1647
+ # [+:disable_joins+]
1648
1648
  # Specifies whether joins should be skipped for an association. If set to true, two or more queries
1649
1649
  # will be generated. Note that in some cases, if order or limit is applied, it will be done in-memory
1650
1650
  # due to database limitations. This option is only applicable on <tt>has_one :through</tt> associations as
1651
1651
  # +has_one+ alone does not perform a join.
1652
- # [:source]
1652
+ # [+:source+]
1653
1653
  # Specifies the source association name used by #has_one <tt>:through</tt> queries.
1654
1654
  # Only use it if the name cannot be inferred from the association.
1655
1655
  # <tt>has_one :favorite, through: :favorites</tt> will look for a
1656
1656
  # <tt>:favorite</tt> on Favorite, unless a <tt>:source</tt> is given.
1657
- # [:source_type]
1657
+ # [+:source_type+]
1658
1658
  # Specifies type of the source association used by #has_one <tt>:through</tt> queries where the source
1659
1659
  # association is a polymorphic #belongs_to.
1660
- # [:validate]
1660
+ # [+:validate+]
1661
1661
  # When set to +true+, validates new objects added to association when saving the parent object. +false+ by default.
1662
1662
  # If you want to ensure associated objects are revalidated on every update, use +validates_associated+.
1663
- # [:autosave]
1663
+ # [+:autosave+]
1664
1664
  # If true, always save the associated object or destroy it if marked for destruction,
1665
1665
  # when saving the parent object. If false, never save or destroy the associated object.
1666
1666
  # By default, only save the associated object if it's a new record.
1667
1667
  #
1668
1668
  # Note that NestedAttributes::ClassMethods#accepts_nested_attributes_for sets
1669
1669
  # <tt>:autosave</tt> to <tt>true</tt>.
1670
- # [:touch]
1670
+ # [+:touch+]
1671
1671
  # If true, the associated object will be touched (the +updated_at+ / +updated_on+ attributes set to current time)
1672
1672
  # when this record is either saved or destroyed. If you specify a symbol, that attribute
1673
1673
  # will be updated with the current time in addition to the +updated_at+ / +updated_on+ attribute.
1674
1674
  # Please note that no validation will be performed when touching, and only the +after_touch+,
1675
1675
  # +after_commit+, and +after_rollback+ callbacks will be executed.
1676
- # [:inverse_of]
1676
+ # [+:inverse_of+]
1677
1677
  # Specifies the name of the #belongs_to association on the associated object
1678
1678
  # that is the inverse of this #has_one association.
1679
1679
  # See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail.
1680
- # [:required]
1680
+ # [+:required+]
1681
1681
  # When set to +true+, the association will also have its presence validated.
1682
1682
  # This will validate the association itself, not the id. You can use
1683
1683
  # +:inverse_of+ to avoid an extra query during validation.
1684
- # [:strict_loading]
1684
+ # [+:strict_loading+]
1685
1685
  # Enforces strict loading every time the associated record is loaded through this association.
1686
- # [:ensuring_owner_was]
1686
+ # [+:ensuring_owner_was+]
1687
1687
  # Specifies an instance method to be called on the owner. The method must return true in order for the
1688
1688
  # associated records to be deleted in a background job.
1689
- # [:query_constraints]
1689
+ # [+:query_constraints+]
1690
1690
  # Serves as a composite foreign key. Defines the list of columns to be used to query the associated object.
1691
1691
  # This is an optional option. By default Rails will attempt to derive the value automatically.
1692
1692
  # When the value is set the Array size must match associated model's primary key or `query_constraints` size.
@@ -1721,28 +1721,28 @@ module ActiveRecord
1721
1721
  # +association+ is a placeholder for the symbol passed as the +name+ argument, so
1722
1722
  # <tt>belongs_to :author</tt> would add among others <tt>author.nil?</tt>.
1723
1723
  #
1724
- # [association]
1724
+ # [<tt>association</tt>]
1725
1725
  # Returns the associated object. +nil+ is returned if none is found.
1726
- # [association=(associate)]
1726
+ # [<tt>association=(associate)</tt>]
1727
1727
  # Assigns the associate object, extracts the primary key, and sets it as the foreign key.
1728
1728
  # No modification or deletion of existing records takes place.
1729
- # [build_association(attributes = {})]
1729
+ # [<tt>build_association(attributes = {})</tt>]
1730
1730
  # Returns a new object of the associated type that has been instantiated
1731
1731
  # with +attributes+ and linked to this object through a foreign key, but has not yet been saved.
1732
- # [create_association(attributes = {})]
1732
+ # [<tt>create_association(attributes = {})</tt>]
1733
1733
  # Returns a new object of the associated type that has been instantiated
1734
1734
  # with +attributes+, linked to this object through a foreign key, and that
1735
1735
  # has already been saved (if it passed the validation).
1736
- # [create_association!(attributes = {})]
1736
+ # [<tt>create_association!(attributes = {})</tt>]
1737
1737
  # Does the same as <tt>create_association</tt>, but raises ActiveRecord::RecordInvalid
1738
1738
  # if the record is invalid.
1739
- # [reload_association]
1739
+ # [<tt>reload_association</tt>]
1740
1740
  # Returns the associated object, forcing a database read.
1741
- # [reset_association]
1741
+ # [<tt>reset_association</tt>]
1742
1742
  # Unloads the associated object. The next access will query it from the database.
1743
- # [association_changed?]
1743
+ # [<tt>association_changed?</tt>]
1744
1744
  # Returns true if a new associate object has been assigned and the next save will update the foreign key.
1745
- # [association_previously_changed?]
1745
+ # [<tt>association_previously_changed?</tt>]
1746
1746
  # Returns true if the previous save updated the association to reference a new associate object.
1747
1747
  #
1748
1748
  # === Example
@@ -1781,11 +1781,11 @@ module ActiveRecord
1781
1781
  #
1782
1782
  # The declaration can also include an +options+ hash to specialize the behavior of the association.
1783
1783
  #
1784
- # [:class_name]
1784
+ # [+:class_name+]
1785
1785
  # Specify the class name of the association. Use it only if that name can't be inferred
1786
1786
  # from the association name. So <tt>belongs_to :author</tt> will by default be linked to the Author class, but
1787
1787
  # if the real class name is Person, you'll have to specify it with this option.
1788
- # [:foreign_key]
1788
+ # [+:foreign_key+]
1789
1789
  # Specify the foreign key used for the association. By default this is guessed to be the name
1790
1790
  # of the association with an "_id" suffix. So a class that defines a <tt>belongs_to :person</tt>
1791
1791
  # association will use "person_id" as the default <tt>:foreign_key</tt>. Similarly,
@@ -1794,22 +1794,22 @@ module ActiveRecord
1794
1794
  #
1795
1795
  # Setting the <tt>:foreign_key</tt> option prevents automatic detection of the association's
1796
1796
  # inverse, so it is generally a good idea to set the <tt>:inverse_of</tt> option as well.
1797
- # [:foreign_type]
1797
+ # [+:foreign_type+]
1798
1798
  # Specify the column used to store the associated object's type, if this is a polymorphic
1799
1799
  # association. By default this is guessed to be the name of the association with a "_type"
1800
1800
  # suffix. So a class that defines a <tt>belongs_to :taggable, polymorphic: true</tt>
1801
1801
  # association will use "taggable_type" as the default <tt>:foreign_type</tt>.
1802
- # [:primary_key]
1802
+ # [+:primary_key+]
1803
1803
  # Specify the method that returns the primary key of associated object used for the association.
1804
1804
  # By default this is +id+.
1805
- # [:dependent]
1805
+ # [+:dependent+]
1806
1806
  # If set to <tt>:destroy</tt>, the associated object is destroyed when this object is. If set to
1807
1807
  # <tt>:delete</tt>, the associated object is deleted *without* calling its destroy method. If set to
1808
1808
  # <tt>:destroy_async</tt>, the associated object is scheduled to be destroyed in a background job.
1809
1809
  # This option should not be specified when #belongs_to is used in conjunction with
1810
1810
  # a #has_many relationship on another class because of the potential to leave
1811
1811
  # orphaned records behind.
1812
- # [:counter_cache]
1812
+ # [+:counter_cache+]
1813
1813
  # Caches the number of belonging objects on the associate class through the use of CounterCache::ClassMethods#increment_counter
1814
1814
  # and CounterCache::ClassMethods#decrement_counter. The counter cache is incremented when an object of this
1815
1815
  # class is created and decremented when it's destroyed. This requires that a column
@@ -1821,14 +1821,14 @@ module ActiveRecord
1821
1821
  # option (e.g., <tt>counter_cache: :my_custom_counter</tt>.)
1822
1822
  # Note: Specifying a counter cache will add it to that model's list of readonly attributes
1823
1823
  # using +attr_readonly+.
1824
- # [:polymorphic]
1824
+ # [+:polymorphic+]
1825
1825
  # Specify this association is a polymorphic association by passing +true+.
1826
1826
  # Note: If you've enabled the counter cache, then you may want to add the counter cache attribute
1827
1827
  # to the +attr_readonly+ list in the associated classes (e.g. <tt>class Post; attr_readonly :comments_count; end</tt>).
1828
- # [:validate]
1828
+ # [+:validate+]
1829
1829
  # When set to +true+, validates new objects added to association when saving the parent object. +false+ by default.
1830
1830
  # If you want to ensure associated objects are revalidated on every update, use +validates_associated+.
1831
- # [:autosave]
1831
+ # [+:autosave+]
1832
1832
  # If true, always save the associated object or destroy it if marked for destruction, when
1833
1833
  # saving the parent object.
1834
1834
  # If false, never save or destroy the associated object.
@@ -1836,34 +1836,34 @@ module ActiveRecord
1836
1836
  #
1837
1837
  # Note that NestedAttributes::ClassMethods#accepts_nested_attributes_for
1838
1838
  # sets <tt>:autosave</tt> to <tt>true</tt>.
1839
- # [:touch]
1839
+ # [+:touch+]
1840
1840
  # If true, the associated object will be touched (the +updated_at+ / +updated_on+ attributes set to current time)
1841
1841
  # when this record is either saved or destroyed. If you specify a symbol, that attribute
1842
1842
  # will be updated with the current time in addition to the +updated_at+ / +updated_on+ attribute.
1843
1843
  # Please note that no validation will be performed when touching, and only the +after_touch+,
1844
1844
  # +after_commit+, and +after_rollback+ callbacks will be executed.
1845
- # [:inverse_of]
1845
+ # [+:inverse_of+]
1846
1846
  # Specifies the name of the #has_one or #has_many association on the associated
1847
1847
  # object that is the inverse of this #belongs_to association.
1848
1848
  # See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail.
1849
- # [:optional]
1849
+ # [+:optional+]
1850
1850
  # When set to +true+, the association will not have its presence validated.
1851
- # [:required]
1851
+ # [+:required+]
1852
1852
  # When set to +true+, the association will also have its presence validated.
1853
1853
  # This will validate the association itself, not the id. You can use
1854
1854
  # +:inverse_of+ to avoid an extra query during validation.
1855
1855
  # NOTE: <tt>required</tt> is set to <tt>true</tt> by default and is deprecated. If
1856
1856
  # you don't want to have association presence validated, use <tt>optional: true</tt>.
1857
- # [:default]
1857
+ # [+:default+]
1858
1858
  # Provide a callable (i.e. proc or lambda) to specify that the association should
1859
1859
  # be initialized with a particular record before validation.
1860
1860
  # Please note that callable won't be executed if the record exists.
1861
- # [:strict_loading]
1861
+ # [+:strict_loading+]
1862
1862
  # Enforces strict loading every time the associated record is loaded through this association.
1863
- # [:ensuring_owner_was]
1863
+ # [+:ensuring_owner_was+]
1864
1864
  # Specifies an instance method to be called on the owner. The method must return true in order for the
1865
1865
  # associated records to be deleted in a background job.
1866
- # [:query_constraints]
1866
+ # [+:query_constraints+]
1867
1867
  # Serves as a composite foreign key. Defines the list of columns to be used to query the associated object.
1868
1868
  # This is an optional option. By default Rails will attempt to derive the value automatically.
1869
1869
  # When the value is set the Array size must match associated model's primary key or `query_constraints` size.
@@ -1920,47 +1920,47 @@ module ActiveRecord
1920
1920
  # +collection+ is a placeholder for the symbol passed as the +name+ argument, so
1921
1921
  # <tt>has_and_belongs_to_many :categories</tt> would add among others <tt>categories.empty?</tt>.
1922
1922
  #
1923
- # [collection]
1923
+ # [<tt>collection</tt>]
1924
1924
  # Returns a Relation of all the associated objects.
1925
1925
  # An empty Relation is returned if none are found.
1926
- # [collection<<(object, ...)]
1926
+ # [<tt>collection<<(object, ...)</tt>]
1927
1927
  # Adds one or more objects to the collection by creating associations in the join table
1928
1928
  # (<tt>collection.push</tt> and <tt>collection.concat</tt> are aliases to this method).
1929
1929
  # Note that this operation instantly fires update SQL without waiting for the save or update call on the
1930
1930
  # parent object, unless the parent object is a new record.
1931
- # [collection.delete(object, ...)]
1931
+ # [<tt>collection.delete(object, ...)</tt>]
1932
1932
  # Removes one or more objects from the collection by removing their associations from the join table.
1933
1933
  # This does not destroy the objects.
1934
- # [collection.destroy(object, ...)]
1934
+ # [<tt>collection.destroy(object, ...)</tt>]
1935
1935
  # Removes one or more objects from the collection by running destroy on each association in the join table, overriding any dependent option.
1936
1936
  # This does not destroy the objects.
1937
- # [collection=objects]
1937
+ # [<tt>collection=objects</tt>]
1938
1938
  # Replaces the collection's content by deleting and adding objects as appropriate.
1939
- # [collection_singular_ids]
1939
+ # [<tt>collection_singular_ids</tt>]
1940
1940
  # Returns an array of the associated objects' ids.
1941
- # [collection_singular_ids=ids]
1941
+ # [<tt>collection_singular_ids=ids</tt>]
1942
1942
  # Replace the collection by the objects identified by the primary keys in +ids+.
1943
- # [collection.clear]
1943
+ # [<tt>collection.clear</tt>]
1944
1944
  # Removes every object from the collection. This does not destroy the objects.
1945
- # [collection.empty?]
1945
+ # [<tt>collection.empty?</tt>]
1946
1946
  # Returns +true+ if there are no associated objects.
1947
- # [collection.size]
1947
+ # [<tt>collection.size</tt>]
1948
1948
  # Returns the number of associated objects.
1949
- # [collection.find(id)]
1949
+ # [<tt>collection.find(id)</tt>]
1950
1950
  # Finds an associated object responding to the +id+ and that
1951
1951
  # meets the condition that it has to be associated with this object.
1952
1952
  # Uses the same rules as ActiveRecord::FinderMethods#find.
1953
- # [collection.exists?(...)]
1953
+ # [<tt>collection.exists?(...)</tt>]
1954
1954
  # Checks whether an associated object with the given conditions exists.
1955
1955
  # Uses the same rules as ActiveRecord::FinderMethods#exists?.
1956
- # [collection.build(attributes = {})]
1956
+ # [<tt>collection.build(attributes = {})</tt>]
1957
1957
  # Returns a new object of the collection type that has been instantiated
1958
1958
  # with +attributes+ and linked to this object through the join table, but has not yet been saved.
1959
- # [collection.create(attributes = {})]
1959
+ # [<tt>collection.create(attributes = {})</tt>]
1960
1960
  # Returns a new object of the collection type that has been instantiated
1961
1961
  # with +attributes+, linked to this object through the join table, and that has already been
1962
1962
  # saved (if it passed the validation).
1963
- # [collection.reload]
1963
+ # [<tt>collection.reload</tt>]
1964
1964
  # Returns a Relation of all of the associated objects, forcing a database read.
1965
1965
  # An empty Relation is returned if none are found.
1966
1966
  #
@@ -2022,15 +2022,15 @@ module ActiveRecord
2022
2022
  #
2023
2023
  # === Options
2024
2024
  #
2025
- # [:class_name]
2025
+ # [+:class_name+]
2026
2026
  # Specify the class name of the association. Use it only if that name can't be inferred
2027
2027
  # from the association name. So <tt>has_and_belongs_to_many :projects</tt> will by default be linked to the
2028
2028
  # Project class, but if the real class name is SuperProject, you'll have to specify it with this option.
2029
- # [:join_table]
2029
+ # [+:join_table+]
2030
2030
  # Specify the name of the join table if the default based on lexical order isn't what you want.
2031
2031
  # <b>WARNING:</b> If you're overwriting the table name of either class, the +table_name+ method
2032
2032
  # MUST be declared underneath any #has_and_belongs_to_many declaration in order to work.
2033
- # [:foreign_key]
2033
+ # [+:foreign_key+]
2034
2034
  # Specify the foreign key used for the association. By default this is guessed to be the name
2035
2035
  # of this class in lower-case and "_id" suffixed. So a Person class that makes
2036
2036
  # a #has_and_belongs_to_many association to Project will use "person_id" as the
@@ -2038,15 +2038,15 @@ module ActiveRecord
2038
2038
  #
2039
2039
  # Setting the <tt>:foreign_key</tt> option prevents automatic detection of the association's
2040
2040
  # inverse, so it is generally a good idea to set the <tt>:inverse_of</tt> option as well.
2041
- # [:association_foreign_key]
2041
+ # [+:association_foreign_key+]
2042
2042
  # Specify the foreign key used for the association on the receiving side of the association.
2043
2043
  # By default this is guessed to be the name of the associated class in lower-case and "_id" suffixed.
2044
2044
  # So if a Person class makes a #has_and_belongs_to_many association to Project,
2045
2045
  # the association will use "project_id" as the default <tt>:association_foreign_key</tt>.
2046
- # [:validate]
2046
+ # [+:validate+]
2047
2047
  # When set to +true+, validates new objects added to association when saving the parent object. +true+ by default.
2048
2048
  # If you want to ensure associated objects are revalidated on every update, use +validates_associated+.
2049
- # [:autosave]
2049
+ # [+:autosave+]
2050
2050
  # If true, always save the associated objects or destroy them if marked for destruction, when
2051
2051
  # saving the parent object.
2052
2052
  # If false, never save or destroy the associated objects.
@@ -2054,7 +2054,7 @@ module ActiveRecord
2054
2054
  #
2055
2055
  # Note that NestedAttributes::ClassMethods#accepts_nested_attributes_for sets
2056
2056
  # <tt>:autosave</tt> to <tt>true</tt>.
2057
- # [:strict_loading]
2057
+ # [+:strict_loading+]
2058
2058
  # Enforces strict loading every time an associated record is loaded through this association.
2059
2059
  #
2060
2060
  # Option examples:
@@ -633,7 +633,20 @@ module ActiveRecord
633
633
  end
634
634
  end
635
635
 
636
- def sql_for_insert(sql, _pk, binds, _returning)
636
+ def sql_for_insert(sql, pk, binds, returning) # :nodoc:
637
+ if supports_insert_returning?
638
+ if pk.nil?
639
+ # Extract the table from the insert sql. Yuck.
640
+ table_ref = extract_table_ref_from_insert_sql(sql)
641
+ pk = primary_key(table_ref) if table_ref
642
+ end
643
+
644
+ returning_columns = returning || Array(pk)
645
+
646
+ returning_columns_statement = returning_columns.map { |c| quote_column_name(c) }.join(", ")
647
+ sql = "#{sql} RETURNING #{returning_columns_statement}" if returning_columns.any?
648
+ end
649
+
637
650
  [sql, binds]
638
651
  end
639
652
 
@@ -657,6 +670,12 @@ module ActiveRecord
657
670
  relation
658
671
  end
659
672
  end
673
+
674
+ def extract_table_ref_from_insert_sql(sql)
675
+ if sql =~ /into\s("[A-Za-z0-9_."\[\]\s]+"|[A-Za-z0-9_."\[\]]+)\s*/im
676
+ $1.strip
677
+ end
678
+ end
660
679
  end
661
680
  end
662
681
  end
@@ -16,7 +16,7 @@ module ActiveRecord
16
16
  delegate :quote_column_name, :quote_table_name, :quote_default_expression, :type_to_sql,
17
17
  :options_include_default?, :supports_indexes_in_create?, :use_foreign_keys?,
18
18
  :quoted_columns_for_index, :supports_partial_index?, :supports_check_constraints?,
19
- :supports_index_include?, :supports_exclusion_constraints?, :supports_unique_keys?,
19
+ :supports_index_include?, :supports_exclusion_constraints?, :supports_unique_constraints?,
20
20
  :supports_nulls_not_distinct?,
21
21
  to: :@conn, private: true
22
22
 
@@ -65,8 +65,8 @@ module ActiveRecord
65
65
  statements.concat(o.exclusion_constraints.map { |exc| accept exc })
66
66
  end
67
67
 
68
- if supports_unique_keys?
69
- statements.concat(o.unique_keys.map { |exc| accept exc })
68
+ if supports_unique_constraints?
69
+ statements.concat(o.unique_constraints.map { |exc| accept exc })
70
70
  end
71
71
 
72
72
  create_sql << "(#{statements.join(', ')})" if statements.present?