activerecord 7.0.8.7 → 7.0.9
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 +86 -0
- data/README.rdoc +1 -0
- data/lib/active_record/associations.rb +117 -117
- data/lib/active_record/attribute_methods/dirty.rb +12 -8
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +7 -3
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +0 -2
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +7 -0
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +2 -1
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -2
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +1 -1
- data/lib/active_record/connection_handling.rb +1 -1
- data/lib/active_record/core.rb +5 -4
- data/lib/active_record/delegated_type.rb +3 -3
- data/lib/active_record/encryption/extended_deterministic_queries.rb +34 -62
- data/lib/active_record/fixtures.rb +12 -0
- data/lib/active_record/gem_version.rb +2 -2
- data/lib/active_record/insert_all.rb +3 -3
- data/lib/active_record/middleware/database_selector.rb +1 -1
- data/lib/active_record/migration.rb +3 -2
- data/lib/active_record/model_schema.rb +5 -3
- data/lib/active_record/nested_attributes.rb +3 -3
- data/lib/active_record/railtie.rb +1 -1
- data/lib/active_record/relation/delegation.rb +1 -1
- data/lib/active_record/relation.rb +1 -1
- data/lib/active_record/timestamp.rb +1 -1
- data/lib/arel/nodes/homogeneous_in.rb +1 -1
- metadata +10 -13
|
@@ -1012,7 +1012,7 @@ module ActiveRecord
|
|
|
1012
1012
|
# query per addressable type.
|
|
1013
1013
|
# For example, if all the addressables are either of class Person or Company, then a total
|
|
1014
1014
|
# of 3 queries will be executed. The list of addressable types to load is determined on
|
|
1015
|
-
# the back of the addresses loaded. This is not supported if Active Record has to
|
|
1015
|
+
# the back of the addresses loaded. This is not supported if Active Record has to fall back
|
|
1016
1016
|
# to the previous implementation of eager loading and will raise ActiveRecord::EagerLoadPolymorphicError.
|
|
1017
1017
|
# The reason is that the parent model's type is a column value so its corresponding table
|
|
1018
1018
|
# name cannot be put in the +FROM+/+JOIN+ clauses of that query.
|
|
@@ -1237,15 +1237,15 @@ module ActiveRecord
|
|
|
1237
1237
|
# +collection+ is a placeholder for the symbol passed as the +name+ argument, so
|
|
1238
1238
|
# <tt>has_many :clients</tt> would add among others <tt>clients.empty?</tt>.
|
|
1239
1239
|
#
|
|
1240
|
-
# [collection]
|
|
1240
|
+
# [<tt>collection</tt>]
|
|
1241
1241
|
# Returns a Relation of all the associated objects.
|
|
1242
1242
|
# An empty Relation is returned if none are found.
|
|
1243
|
-
# [collection<<(object, ...)]
|
|
1243
|
+
# [<tt>collection<<(object, ...)</tt>]
|
|
1244
1244
|
# Adds one or more objects to the collection by setting their foreign keys to the collection's primary key.
|
|
1245
1245
|
# Note that this operation instantly fires update SQL without waiting for the save or update call on the
|
|
1246
1246
|
# parent object, unless the parent object is a new record.
|
|
1247
1247
|
# This will also run validations and callbacks of associated object(s).
|
|
1248
|
-
# [collection.delete(object, ...)]
|
|
1248
|
+
# [<tt>collection.delete(object, ...)</tt>]
|
|
1249
1249
|
# Removes one or more objects from the collection by setting their foreign keys to +NULL+.
|
|
1250
1250
|
# Objects will be in addition destroyed if they're associated with <tt>dependent: :destroy</tt>,
|
|
1251
1251
|
# and deleted if they're associated with <tt>dependent: :delete_all</tt>.
|
|
@@ -1253,54 +1253,54 @@ module ActiveRecord
|
|
|
1253
1253
|
# If the <tt>:through</tt> option is used, then the join records are deleted (rather than
|
|
1254
1254
|
# nullified) by default, but you can specify <tt>dependent: :destroy</tt> or
|
|
1255
1255
|
# <tt>dependent: :nullify</tt> to override this.
|
|
1256
|
-
# [collection.destroy(object, ...)]
|
|
1256
|
+
# [<tt>collection.destroy(object, ...)</tt>]
|
|
1257
1257
|
# Removes one or more objects from the collection by running <tt>destroy</tt> on
|
|
1258
1258
|
# each record, regardless of any dependent option, ensuring callbacks are run.
|
|
1259
1259
|
#
|
|
1260
1260
|
# If the <tt>:through</tt> option is used, then the join records are destroyed
|
|
1261
1261
|
# instead, not the objects themselves.
|
|
1262
|
-
# [collection=objects]
|
|
1262
|
+
# [<tt>collection=objects</tt>]
|
|
1263
1263
|
# Replaces the collections content by deleting and adding objects as appropriate. If the <tt>:through</tt>
|
|
1264
1264
|
# option is true callbacks in the join models are triggered except destroy callbacks, since deletion is
|
|
1265
1265
|
# direct by default. You can specify <tt>dependent: :destroy</tt> or
|
|
1266
1266
|
# <tt>dependent: :nullify</tt> to override this.
|
|
1267
|
-
# [collection_singular_ids]
|
|
1267
|
+
# [<tt>collection_singular_ids</tt>]
|
|
1268
1268
|
# Returns an array of the associated objects' ids
|
|
1269
|
-
# [collection_singular_ids=ids]
|
|
1269
|
+
# [<tt>collection_singular_ids=ids</tt>]
|
|
1270
1270
|
# Replace the collection with the objects identified by the primary keys in +ids+. This
|
|
1271
1271
|
# method loads the models and calls <tt>collection=</tt>. See above.
|
|
1272
|
-
# [collection.clear]
|
|
1272
|
+
# [<tt>collection.clear</tt>]
|
|
1273
1273
|
# Removes every object from the collection. This destroys the associated objects if they
|
|
1274
1274
|
# are associated with <tt>dependent: :destroy</tt>, deletes them directly from the
|
|
1275
1275
|
# database if <tt>dependent: :delete_all</tt>, otherwise sets their foreign keys to +NULL+.
|
|
1276
1276
|
# If the <tt>:through</tt> option is true no destroy callbacks are invoked on the join models.
|
|
1277
1277
|
# Join models are directly deleted.
|
|
1278
|
-
# [collection.empty
|
|
1278
|
+
# [<tt>collection.empty?</tt>]
|
|
1279
1279
|
# Returns +true+ if there are no associated objects.
|
|
1280
|
-
# [collection.size]
|
|
1280
|
+
# [<tt>collection.size</tt>]
|
|
1281
1281
|
# Returns the number of associated objects.
|
|
1282
|
-
# [collection.find(...)]
|
|
1282
|
+
# [<tt>collection.find(...)</tt>]
|
|
1283
1283
|
# Finds an associated object according to the same rules as ActiveRecord::FinderMethods#find.
|
|
1284
|
-
# [collection.exists?(...)]
|
|
1284
|
+
# [<tt>collection.exists?(...)</tt>]
|
|
1285
1285
|
# Checks whether an associated object with the given conditions exists.
|
|
1286
1286
|
# Uses the same rules as ActiveRecord::FinderMethods#exists?.
|
|
1287
|
-
# [collection.build(attributes = {}, ...)]
|
|
1287
|
+
# [<tt>collection.build(attributes = {}, ...)</tt>]
|
|
1288
1288
|
# Returns one or more new objects of the collection type that have been instantiated
|
|
1289
1289
|
# with +attributes+ and linked to this object through a foreign key, but have not yet
|
|
1290
1290
|
# been saved.
|
|
1291
|
-
# [collection.create(attributes = {})]
|
|
1291
|
+
# [<tt>collection.create(attributes = {})</tt>]
|
|
1292
1292
|
# Returns a new object of the collection type that has been instantiated
|
|
1293
1293
|
# with +attributes+, linked to this object through a foreign key, and that has already
|
|
1294
1294
|
# been saved (if it passed the validation). *Note*: This only works if the base model
|
|
1295
1295
|
# already exists in the DB, not if it is a new (unsaved) record!
|
|
1296
|
-
# [collection.create!(attributes = {})]
|
|
1296
|
+
# [<tt>collection.create!(attributes = {})</tt>]
|
|
1297
1297
|
# Does the same as <tt>collection.create</tt>, but raises ActiveRecord::RecordInvalid
|
|
1298
1298
|
# if the record is invalid.
|
|
1299
|
-
# [collection.reload]
|
|
1299
|
+
# [<tt>collection.reload</tt>]
|
|
1300
1300
|
# Returns a Relation of all of the associated objects, forcing a database read.
|
|
1301
1301
|
# An empty Relation is returned if none are found.
|
|
1302
1302
|
#
|
|
1303
|
-
#
|
|
1303
|
+
# ==== Example
|
|
1304
1304
|
#
|
|
1305
1305
|
# A <tt>Firm</tt> class declares <tt>has_many :clients</tt>, which will add:
|
|
1306
1306
|
# * <tt>Firm#clients</tt> (similar to <tt>Client.where(firm_id: id)</tt>)
|
|
@@ -1321,7 +1321,7 @@ module ActiveRecord
|
|
|
1321
1321
|
# * <tt>Firm#clients.reload</tt>
|
|
1322
1322
|
# The declaration can also include an +options+ hash to specialize the behavior of the association.
|
|
1323
1323
|
#
|
|
1324
|
-
#
|
|
1324
|
+
# ==== Scopes
|
|
1325
1325
|
#
|
|
1326
1326
|
# You can pass a second argument +scope+ as a callable (i.e. proc or
|
|
1327
1327
|
# lambda) to retrieve a specific set of records or customize the generated
|
|
@@ -1332,7 +1332,7 @@ module ActiveRecord
|
|
|
1332
1332
|
# has_many :employees, -> { joins(:address) }
|
|
1333
1333
|
# has_many :posts, ->(blog) { where("max_post_length > ?", blog.max_post_length) }
|
|
1334
1334
|
#
|
|
1335
|
-
#
|
|
1335
|
+
# ==== Extensions
|
|
1336
1336
|
#
|
|
1337
1337
|
# The +extension+ argument allows you to pass a block into a has_many
|
|
1338
1338
|
# association. This is useful for adding new finders, creators, and other
|
|
@@ -1346,28 +1346,28 @@ module ActiveRecord
|
|
|
1346
1346
|
# end
|
|
1347
1347
|
# end
|
|
1348
1348
|
#
|
|
1349
|
-
#
|
|
1350
|
-
# [
|
|
1349
|
+
# ==== Options
|
|
1350
|
+
# [+:class_name+]
|
|
1351
1351
|
# Specify the class name of the association. Use it only if that name can't be inferred
|
|
1352
1352
|
# from the association name. So <tt>has_many :products</tt> will by default be linked
|
|
1353
1353
|
# to the +Product+ class, but if the real class name is +SpecialProduct+, you'll have to
|
|
1354
1354
|
# specify it with this option.
|
|
1355
|
-
# [
|
|
1355
|
+
# [+:foreign_key+]
|
|
1356
1356
|
# Specify the foreign key used for the association. By default this is guessed to be the name
|
|
1357
1357
|
# of this class in lower-case and "_id" suffixed. So a Person class that makes a #has_many
|
|
1358
1358
|
# association will use "person_id" as the default <tt>:foreign_key</tt>.
|
|
1359
1359
|
#
|
|
1360
1360
|
# If you are going to modify the association (rather than just read from it), then it is
|
|
1361
1361
|
# a good idea to set the <tt>:inverse_of</tt> option.
|
|
1362
|
-
# [
|
|
1362
|
+
# [+:foreign_type+]
|
|
1363
1363
|
# Specify the column used to store the associated object's type, if this is a polymorphic
|
|
1364
1364
|
# association. By default this is guessed to be the name of the polymorphic association
|
|
1365
1365
|
# specified on "as" option with a "_type" suffix. So a class that defines a
|
|
1366
1366
|
# <tt>has_many :tags, as: :taggable</tt> association will use "taggable_type" as the
|
|
1367
1367
|
# default <tt>:foreign_type</tt>.
|
|
1368
|
-
# [
|
|
1368
|
+
# [+:primary_key+]
|
|
1369
1369
|
# Specify the name of the column to use as the primary key for the association. By default this is +id+.
|
|
1370
|
-
# [
|
|
1370
|
+
# [+:dependent+]
|
|
1371
1371
|
# Controls what happens to the associated objects when
|
|
1372
1372
|
# their owner is destroyed. Note that these are implemented as
|
|
1373
1373
|
# callbacks, and Rails executes callbacks in order. Therefore, other
|
|
@@ -1394,12 +1394,12 @@ module ActiveRecord
|
|
|
1394
1394
|
# <tt>has_many :comments, -> { where published: true }, dependent: :destroy</tt> and <tt>destroy</tt> is
|
|
1395
1395
|
# called on a post, only published comments are destroyed. This means that any unpublished comments in the
|
|
1396
1396
|
# database would still contain a foreign key pointing to the now deleted post.
|
|
1397
|
-
# [
|
|
1397
|
+
# [+:counter_cache+]
|
|
1398
1398
|
# This option can be used to configure a custom named <tt>:counter_cache.</tt> You only need this option,
|
|
1399
1399
|
# when you customized the name of your <tt>:counter_cache</tt> on the #belongs_to association.
|
|
1400
|
-
# [
|
|
1400
|
+
# [+:as+]
|
|
1401
1401
|
# Specifies a polymorphic interface (See #belongs_to).
|
|
1402
|
-
# [
|
|
1402
|
+
# [+:through+]
|
|
1403
1403
|
# Specifies an association through which to perform the query. This can be any other type
|
|
1404
1404
|
# of association, including other <tt>:through</tt> associations. Options for <tt>:class_name</tt>,
|
|
1405
1405
|
# <tt>:primary_key</tt> and <tt>:foreign_key</tt> are ignored, as the association uses the
|
|
@@ -1415,23 +1415,23 @@ module ActiveRecord
|
|
|
1415
1415
|
# join model. This allows associated records to be built which will automatically create
|
|
1416
1416
|
# the appropriate join model records when they are saved. (See the 'Association Join Models'
|
|
1417
1417
|
# section above.)
|
|
1418
|
-
# [
|
|
1418
|
+
# [+:disable_joins+]
|
|
1419
1419
|
# Specifies whether joins should be skipped for an association. If set to true, two or more queries
|
|
1420
1420
|
# will be generated. Note that in some cases, if order or limit is applied, it will be done in-memory
|
|
1421
1421
|
# due to database limitations. This option is only applicable on <tt>has_many :through</tt> associations as
|
|
1422
1422
|
# +has_many+ alone do not perform a join.
|
|
1423
|
-
# [
|
|
1423
|
+
# [+:source+]
|
|
1424
1424
|
# Specifies the source association name used by #has_many <tt>:through</tt> queries.
|
|
1425
1425
|
# Only use it if the name cannot be inferred from the association.
|
|
1426
1426
|
# <tt>has_many :subscribers, through: :subscriptions</tt> will look for either <tt>:subscribers</tt> or
|
|
1427
1427
|
# <tt>:subscriber</tt> on Subscription, unless a <tt>:source</tt> is given.
|
|
1428
|
-
# [
|
|
1428
|
+
# [+:source_type+]
|
|
1429
1429
|
# Specifies type of the source association used by #has_many <tt>:through</tt> queries where the source
|
|
1430
1430
|
# association is a polymorphic #belongs_to.
|
|
1431
|
-
# [
|
|
1431
|
+
# [+:validate+]
|
|
1432
1432
|
# When set to +true+, validates new objects added to association when saving the parent object. +true+ by default.
|
|
1433
1433
|
# If you want to ensure associated objects are revalidated on every update, use +validates_associated+.
|
|
1434
|
-
# [
|
|
1434
|
+
# [+:autosave+]
|
|
1435
1435
|
# If true, always save the associated objects or destroy them if marked for destruction,
|
|
1436
1436
|
# when saving the parent object. If false, never save or destroy the associated objects.
|
|
1437
1437
|
# By default, only save associated objects that are new records. This option is implemented as a
|
|
@@ -1440,18 +1440,18 @@ module ActiveRecord
|
|
|
1440
1440
|
#
|
|
1441
1441
|
# Note that NestedAttributes::ClassMethods#accepts_nested_attributes_for sets
|
|
1442
1442
|
# <tt>:autosave</tt> to <tt>true</tt>.
|
|
1443
|
-
# [
|
|
1443
|
+
# [+:inverse_of+]
|
|
1444
1444
|
# Specifies the name of the #belongs_to association on the associated object
|
|
1445
1445
|
# that is the inverse of this #has_many association.
|
|
1446
1446
|
# See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail.
|
|
1447
|
-
# [
|
|
1447
|
+
# [+:extend+]
|
|
1448
1448
|
# Specifies a module or array of modules that will be extended into the association object returned.
|
|
1449
1449
|
# Useful for defining methods on associations, especially when they should be shared between multiple
|
|
1450
1450
|
# association objects.
|
|
1451
|
-
# [
|
|
1451
|
+
# [+:strict_loading+]
|
|
1452
1452
|
# When set to +true+, enforces strict loading every time the associated record is loaded through this
|
|
1453
1453
|
# association.
|
|
1454
|
-
# [
|
|
1454
|
+
# [+:ensuring_owner_was+]
|
|
1455
1455
|
# Specifies an instance method to be called on the owner. The method must return true in order for the
|
|
1456
1456
|
# associated records to be deleted in a background job.
|
|
1457
1457
|
#
|
|
@@ -1481,27 +1481,27 @@ module ActiveRecord
|
|
|
1481
1481
|
# +association+ is a placeholder for the symbol passed as the +name+ argument, so
|
|
1482
1482
|
# <tt>has_one :manager</tt> would add among others <tt>manager.nil?</tt>.
|
|
1483
1483
|
#
|
|
1484
|
-
# [association]
|
|
1484
|
+
# [<tt>association</tt>]
|
|
1485
1485
|
# Returns the associated object. +nil+ is returned if none is found.
|
|
1486
|
-
# [association=(associate)]
|
|
1486
|
+
# [<tt>association=(associate)</tt>]
|
|
1487
1487
|
# Assigns the associate object, extracts the primary key, sets it as the foreign key,
|
|
1488
1488
|
# and saves the associate object. To avoid database inconsistencies, permanently deletes an existing
|
|
1489
1489
|
# associated object when assigning a new one, even if the new one isn't saved to database.
|
|
1490
|
-
# [build_association(attributes = {})]
|
|
1490
|
+
# [<tt>build_association(attributes = {})</tt>]
|
|
1491
1491
|
# Returns a new object of the associated type that has been instantiated
|
|
1492
1492
|
# with +attributes+ and linked to this object through a foreign key, but has not
|
|
1493
1493
|
# yet been saved.
|
|
1494
|
-
# [create_association(attributes = {})]
|
|
1494
|
+
# [<tt>create_association(attributes = {})</tt>]
|
|
1495
1495
|
# Returns a new object of the associated type that has been instantiated
|
|
1496
1496
|
# with +attributes+, linked to this object through a foreign key, and that
|
|
1497
1497
|
# has already been saved (if it passed the validation).
|
|
1498
|
-
# [create_association!(attributes = {})]
|
|
1498
|
+
# [<tt>create_association!(attributes = {})</tt>]
|
|
1499
1499
|
# Does the same as <tt>create_association</tt>, but raises ActiveRecord::RecordInvalid
|
|
1500
1500
|
# if the record is invalid.
|
|
1501
|
-
# [reload_association]
|
|
1501
|
+
# [<tt>reload_association</tt>]
|
|
1502
1502
|
# Returns the associated object, forcing a database read.
|
|
1503
1503
|
#
|
|
1504
|
-
#
|
|
1504
|
+
# ==== Example
|
|
1505
1505
|
#
|
|
1506
1506
|
# An Account class declares <tt>has_one :beneficiary</tt>, which will add:
|
|
1507
1507
|
# * <tt>Account#beneficiary</tt> (similar to <tt>Beneficiary.where(account_id: id).first</tt>)
|
|
@@ -1511,7 +1511,7 @@ module ActiveRecord
|
|
|
1511
1511
|
# * <tt>Account#create_beneficiary!</tt> (similar to <tt>b = Beneficiary.new(account_id: id); b.save!; b</tt>)
|
|
1512
1512
|
# * <tt>Account#reload_beneficiary</tt>
|
|
1513
1513
|
#
|
|
1514
|
-
#
|
|
1514
|
+
# ==== Scopes
|
|
1515
1515
|
#
|
|
1516
1516
|
# You can pass a second argument +scope+ as a callable (i.e. proc or
|
|
1517
1517
|
# lambda) to retrieve a specific record or customize the generated query
|
|
@@ -1522,16 +1522,16 @@ module ActiveRecord
|
|
|
1522
1522
|
# has_one :employer, -> { joins(:company) }
|
|
1523
1523
|
# has_one :latest_post, ->(blog) { where("created_at > ?", blog.enabled_at) }
|
|
1524
1524
|
#
|
|
1525
|
-
#
|
|
1525
|
+
# ==== Options
|
|
1526
1526
|
#
|
|
1527
1527
|
# The declaration can also include an +options+ hash to specialize the behavior of the association.
|
|
1528
1528
|
#
|
|
1529
1529
|
# Options are:
|
|
1530
|
-
# [
|
|
1530
|
+
# [+:class_name+]
|
|
1531
1531
|
# Specify the class name of the association. Use it only if that name can't be inferred
|
|
1532
1532
|
# from the association name. So <tt>has_one :manager</tt> will by default be linked to the Manager class, but
|
|
1533
1533
|
# if the real class name is Person, you'll have to specify it with this option.
|
|
1534
|
-
# [
|
|
1534
|
+
# [+:dependent+]
|
|
1535
1535
|
# Controls what happens to the associated object when
|
|
1536
1536
|
# its owner is destroyed:
|
|
1537
1537
|
#
|
|
@@ -1547,24 +1547,24 @@ module ActiveRecord
|
|
|
1547
1547
|
# * <tt>:restrict_with_error</tt> causes an error to be added to the owner if there is an associated object
|
|
1548
1548
|
#
|
|
1549
1549
|
# Note that <tt>:dependent</tt> option is ignored when using <tt>:through</tt> option.
|
|
1550
|
-
# [
|
|
1550
|
+
# [+:foreign_key+]
|
|
1551
1551
|
# Specify the foreign key used for the association. By default this is guessed to be the name
|
|
1552
1552
|
# of this class in lower-case and "_id" suffixed. So a Person class that makes a #has_one association
|
|
1553
1553
|
# will use "person_id" as the default <tt>:foreign_key</tt>.
|
|
1554
1554
|
#
|
|
1555
1555
|
# If you are going to modify the association (rather than just read from it), then it is
|
|
1556
1556
|
# a good idea to set the <tt>:inverse_of</tt> option.
|
|
1557
|
-
# [
|
|
1557
|
+
# [+:foreign_type+]
|
|
1558
1558
|
# Specify the column used to store the associated object's type, if this is a polymorphic
|
|
1559
1559
|
# association. By default this is guessed to be the name of the polymorphic association
|
|
1560
1560
|
# specified on "as" option with a "_type" suffix. So a class that defines a
|
|
1561
1561
|
# <tt>has_one :tag, as: :taggable</tt> association will use "taggable_type" as the
|
|
1562
1562
|
# default <tt>:foreign_type</tt>.
|
|
1563
|
-
# [
|
|
1563
|
+
# [+:primary_key+]
|
|
1564
1564
|
# Specify the method that returns the primary key used for the association. By default this is +id+.
|
|
1565
|
-
# [
|
|
1565
|
+
# [+:as+]
|
|
1566
1566
|
# Specifies a polymorphic interface (See #belongs_to).
|
|
1567
|
-
# [
|
|
1567
|
+
# [+:through+]
|
|
1568
1568
|
# Specifies a Join Model through which to perform the query. Options for <tt>:class_name</tt>,
|
|
1569
1569
|
# <tt>:primary_key</tt>, and <tt>:foreign_key</tt> are ignored, as the association uses the
|
|
1570
1570
|
# source reflection. You can only use a <tt>:through</tt> query through a #has_one
|
|
@@ -1580,46 +1580,46 @@ module ActiveRecord
|
|
|
1580
1580
|
# join model. This allows associated records to be built which will automatically create
|
|
1581
1581
|
# the appropriate join model records when they are saved. (See the 'Association Join Models'
|
|
1582
1582
|
# section above.)
|
|
1583
|
-
# [
|
|
1583
|
+
# [+:disable_joins+]
|
|
1584
1584
|
# Specifies whether joins should be skipped for an association. If set to true, two or more queries
|
|
1585
1585
|
# will be generated. Note that in some cases, if order or limit is applied, it will be done in-memory
|
|
1586
1586
|
# due to database limitations. This option is only applicable on <tt>has_one :through</tt> associations as
|
|
1587
1587
|
# +has_one+ alone does not perform a join.
|
|
1588
|
-
# [
|
|
1588
|
+
# [+:source+]
|
|
1589
1589
|
# Specifies the source association name used by #has_one <tt>:through</tt> queries.
|
|
1590
1590
|
# Only use it if the name cannot be inferred from the association.
|
|
1591
1591
|
# <tt>has_one :favorite, through: :favorites</tt> will look for a
|
|
1592
1592
|
# <tt>:favorite</tt> on Favorite, unless a <tt>:source</tt> is given.
|
|
1593
|
-
# [
|
|
1593
|
+
# [+:source_type+]
|
|
1594
1594
|
# Specifies type of the source association used by #has_one <tt>:through</tt> queries where the source
|
|
1595
1595
|
# association is a polymorphic #belongs_to.
|
|
1596
|
-
# [
|
|
1596
|
+
# [+:validate+]
|
|
1597
1597
|
# When set to +true+, validates new objects added to association when saving the parent object. +false+ by default.
|
|
1598
1598
|
# If you want to ensure associated objects are revalidated on every update, use +validates_associated+.
|
|
1599
|
-
# [
|
|
1599
|
+
# [+:autosave+]
|
|
1600
1600
|
# If true, always save the associated object or destroy it if marked for destruction,
|
|
1601
1601
|
# when saving the parent object. If false, never save or destroy the associated object.
|
|
1602
1602
|
# By default, only save the associated object if it's a new record.
|
|
1603
1603
|
#
|
|
1604
1604
|
# Note that NestedAttributes::ClassMethods#accepts_nested_attributes_for sets
|
|
1605
1605
|
# <tt>:autosave</tt> to <tt>true</tt>.
|
|
1606
|
-
# [
|
|
1606
|
+
# [+:touch+]
|
|
1607
1607
|
# If true, the associated object will be touched (the +updated_at+ / +updated_on+ attributes set to current time)
|
|
1608
1608
|
# when this record is either saved or destroyed. If you specify a symbol, that attribute
|
|
1609
1609
|
# will be updated with the current time in addition to the +updated_at+ / +updated_on+ attribute.
|
|
1610
1610
|
# Please note that no validation will be performed when touching, and only the +after_touch+,
|
|
1611
1611
|
# +after_commit+, and +after_rollback+ callbacks will be executed.
|
|
1612
|
-
# [
|
|
1612
|
+
# [+:inverse_of+]
|
|
1613
1613
|
# Specifies the name of the #belongs_to association on the associated object
|
|
1614
1614
|
# that is the inverse of this #has_one association.
|
|
1615
1615
|
# See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail.
|
|
1616
|
-
# [
|
|
1616
|
+
# [+:required+]
|
|
1617
1617
|
# When set to +true+, the association will also have its presence validated.
|
|
1618
1618
|
# This will validate the association itself, not the id. You can use
|
|
1619
1619
|
# +:inverse_of+ to avoid an extra query during validation.
|
|
1620
|
-
# [
|
|
1620
|
+
# [+:strict_loading+]
|
|
1621
1621
|
# Enforces strict loading every time the associated record is loaded through this association.
|
|
1622
|
-
# [
|
|
1622
|
+
# [+:ensuring_owner_was+]
|
|
1623
1623
|
# Specifies an instance method to be called on the owner. The method must return true in order for the
|
|
1624
1624
|
# associated records to be deleted in a background job.
|
|
1625
1625
|
#
|
|
@@ -1652,29 +1652,29 @@ module ActiveRecord
|
|
|
1652
1652
|
# +association+ is a placeholder for the symbol passed as the +name+ argument, so
|
|
1653
1653
|
# <tt>belongs_to :author</tt> would add among others <tt>author.nil?</tt>.
|
|
1654
1654
|
#
|
|
1655
|
-
# [association]
|
|
1655
|
+
# [<tt>association</tt>]
|
|
1656
1656
|
# Returns the associated object. +nil+ is returned if none is found.
|
|
1657
|
-
# [association=(associate)]
|
|
1657
|
+
# [<tt>association=(associate)</tt>]
|
|
1658
1658
|
# Assigns the associate object, extracts the primary key, and sets it as the foreign key.
|
|
1659
1659
|
# No modification or deletion of existing records takes place.
|
|
1660
|
-
# [build_association(attributes = {})]
|
|
1660
|
+
# [<tt>build_association(attributes = {})</tt>]
|
|
1661
1661
|
# Returns a new object of the associated type that has been instantiated
|
|
1662
1662
|
# with +attributes+ and linked to this object through a foreign key, but has not yet been saved.
|
|
1663
|
-
# [create_association(attributes = {})]
|
|
1663
|
+
# [<tt>create_association(attributes = {})</tt>]
|
|
1664
1664
|
# Returns a new object of the associated type that has been instantiated
|
|
1665
1665
|
# with +attributes+, linked to this object through a foreign key, and that
|
|
1666
1666
|
# has already been saved (if it passed the validation).
|
|
1667
|
-
# [create_association!(attributes = {})]
|
|
1667
|
+
# [<tt>create_association!(attributes = {})</tt>]
|
|
1668
1668
|
# Does the same as <tt>create_association</tt>, but raises ActiveRecord::RecordInvalid
|
|
1669
1669
|
# if the record is invalid.
|
|
1670
|
-
# [reload_association]
|
|
1670
|
+
# [<tt>reload_association</tt>]
|
|
1671
1671
|
# Returns the associated object, forcing a database read.
|
|
1672
|
-
# [association_changed
|
|
1672
|
+
# [<tt>association_changed?</tt>]
|
|
1673
1673
|
# Returns true if a new associate object has been assigned and the next save will update the foreign key.
|
|
1674
|
-
# [association_previously_changed
|
|
1674
|
+
# [<tt>association_previously_changed?</tt>]
|
|
1675
1675
|
# Returns true if the previous save updated the association to reference a new associate object.
|
|
1676
1676
|
#
|
|
1677
|
-
#
|
|
1677
|
+
# ==== Example
|
|
1678
1678
|
#
|
|
1679
1679
|
# A Post class declares <tt>belongs_to :author</tt>, which will add:
|
|
1680
1680
|
# * <tt>Post#author</tt> (similar to <tt>Author.find(author_id)</tt>)
|
|
@@ -1687,7 +1687,7 @@ module ActiveRecord
|
|
|
1687
1687
|
# * <tt>Post#author_previously_changed?</tt>
|
|
1688
1688
|
# The declaration can also include an +options+ hash to specialize the behavior of the association.
|
|
1689
1689
|
#
|
|
1690
|
-
#
|
|
1690
|
+
# ==== Scopes
|
|
1691
1691
|
#
|
|
1692
1692
|
# You can pass a second argument +scope+ as a callable (i.e. proc or
|
|
1693
1693
|
# lambda) to retrieve a specific record or customize the generated query
|
|
@@ -1698,13 +1698,13 @@ module ActiveRecord
|
|
|
1698
1698
|
# belongs_to :user, -> { joins(:friends) }
|
|
1699
1699
|
# belongs_to :level, ->(game) { where("game_level > ?", game.current_level) }
|
|
1700
1700
|
#
|
|
1701
|
-
#
|
|
1701
|
+
# ==== Options
|
|
1702
1702
|
#
|
|
1703
|
-
# [
|
|
1703
|
+
# [+:class_name+]
|
|
1704
1704
|
# Specify the class name of the association. Use it only if that name can't be inferred
|
|
1705
1705
|
# from the association name. So <tt>belongs_to :author</tt> will by default be linked to the Author class, but
|
|
1706
1706
|
# if the real class name is Person, you'll have to specify it with this option.
|
|
1707
|
-
# [
|
|
1707
|
+
# [+:foreign_key+]
|
|
1708
1708
|
# Specify the foreign key used for the association. By default this is guessed to be the name
|
|
1709
1709
|
# of the association with an "_id" suffix. So a class that defines a <tt>belongs_to :person</tt>
|
|
1710
1710
|
# association will use "person_id" as the default <tt>:foreign_key</tt>. Similarly,
|
|
@@ -1713,22 +1713,22 @@ module ActiveRecord
|
|
|
1713
1713
|
#
|
|
1714
1714
|
# If you are going to modify the association (rather than just read from it), then it is
|
|
1715
1715
|
# a good idea to set the <tt>:inverse_of</tt> option.
|
|
1716
|
-
# [
|
|
1716
|
+
# [+:foreign_type+]
|
|
1717
1717
|
# Specify the column used to store the associated object's type, if this is a polymorphic
|
|
1718
1718
|
# association. By default this is guessed to be the name of the association with a "_type"
|
|
1719
1719
|
# suffix. So a class that defines a <tt>belongs_to :taggable, polymorphic: true</tt>
|
|
1720
1720
|
# association will use "taggable_type" as the default <tt>:foreign_type</tt>.
|
|
1721
|
-
# [
|
|
1721
|
+
# [+:primary_key+]
|
|
1722
1722
|
# Specify the method that returns the primary key of associated object used for the association.
|
|
1723
1723
|
# By default this is +id+.
|
|
1724
|
-
# [
|
|
1724
|
+
# [+:dependent+]
|
|
1725
1725
|
# If set to <tt>:destroy</tt>, the associated object is destroyed when this object is. If set to
|
|
1726
1726
|
# <tt>:delete</tt>, the associated object is deleted *without* calling its destroy method. If set to
|
|
1727
1727
|
# <tt>:destroy_async</tt>, the associated object is scheduled to be destroyed in a background job.
|
|
1728
1728
|
# This option should not be specified when #belongs_to is used in conjunction with
|
|
1729
1729
|
# a #has_many relationship on another class because of the potential to leave
|
|
1730
1730
|
# orphaned records behind.
|
|
1731
|
-
# [
|
|
1731
|
+
# [+:counter_cache+]
|
|
1732
1732
|
# Caches the number of belonging objects on the associate class through the use of CounterCache::ClassMethods#increment_counter
|
|
1733
1733
|
# and CounterCache::ClassMethods#decrement_counter. The counter cache is incremented when an object of this
|
|
1734
1734
|
# class is created and decremented when it's destroyed. This requires that a column
|
|
@@ -1740,14 +1740,14 @@ module ActiveRecord
|
|
|
1740
1740
|
# option (e.g., <tt>counter_cache: :my_custom_counter</tt>.)
|
|
1741
1741
|
# Note: Specifying a counter cache will add it to that model's list of readonly attributes
|
|
1742
1742
|
# using +attr_readonly+.
|
|
1743
|
-
# [
|
|
1743
|
+
# [+:polymorphic+]
|
|
1744
1744
|
# Specify this association is a polymorphic association by passing +true+.
|
|
1745
1745
|
# Note: If you've enabled the counter cache, then you may want to add the counter cache attribute
|
|
1746
1746
|
# to the +attr_readonly+ list in the associated classes (e.g. <tt>class Post; attr_readonly :comments_count; end</tt>).
|
|
1747
|
-
# [
|
|
1747
|
+
# [+:validate+]
|
|
1748
1748
|
# When set to +true+, validates new objects added to association when saving the parent object. +false+ by default.
|
|
1749
1749
|
# If you want to ensure associated objects are revalidated on every update, use +validates_associated+.
|
|
1750
|
-
# [
|
|
1750
|
+
# [+:autosave+]
|
|
1751
1751
|
# If true, always save the associated object or destroy it if marked for destruction, when
|
|
1752
1752
|
# saving the parent object.
|
|
1753
1753
|
# If false, never save or destroy the associated object.
|
|
@@ -1755,30 +1755,30 @@ module ActiveRecord
|
|
|
1755
1755
|
#
|
|
1756
1756
|
# Note that NestedAttributes::ClassMethods#accepts_nested_attributes_for
|
|
1757
1757
|
# sets <tt>:autosave</tt> to <tt>true</tt>.
|
|
1758
|
-
# [
|
|
1758
|
+
# [+:touch+]
|
|
1759
1759
|
# If true, the associated object will be touched (the +updated_at+ / +updated_on+ attributes set to current time)
|
|
1760
1760
|
# when this record is either saved or destroyed. If you specify a symbol, that attribute
|
|
1761
1761
|
# will be updated with the current time in addition to the +updated_at+ / +updated_on+ attribute.
|
|
1762
1762
|
# Please note that no validation will be performed when touching, and only the +after_touch+,
|
|
1763
1763
|
# +after_commit+, and +after_rollback+ callbacks will be executed.
|
|
1764
|
-
# [
|
|
1764
|
+
# [+:inverse_of+]
|
|
1765
1765
|
# Specifies the name of the #has_one or #has_many association on the associated
|
|
1766
1766
|
# object that is the inverse of this #belongs_to association.
|
|
1767
1767
|
# See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail.
|
|
1768
|
-
# [
|
|
1768
|
+
# [+:optional+]
|
|
1769
1769
|
# When set to +true+, the association will not have its presence validated.
|
|
1770
|
-
# [
|
|
1770
|
+
# [+:required+]
|
|
1771
1771
|
# When set to +true+, the association will also have its presence validated.
|
|
1772
1772
|
# This will validate the association itself, not the id. You can use
|
|
1773
1773
|
# +:inverse_of+ to avoid an extra query during validation.
|
|
1774
1774
|
# NOTE: <tt>required</tt> is set to <tt>true</tt> by default and is deprecated. If
|
|
1775
1775
|
# you don't want to have association presence validated, use <tt>optional: true</tt>.
|
|
1776
|
-
# [
|
|
1776
|
+
# [+:default+]
|
|
1777
1777
|
# Provide a callable (i.e. proc or lambda) to specify that the association should
|
|
1778
1778
|
# be initialized with a particular record before validation.
|
|
1779
|
-
# [
|
|
1779
|
+
# [+:strict_loading+]
|
|
1780
1780
|
# Enforces strict loading every time the associated record is loaded through this association.
|
|
1781
|
-
# [
|
|
1781
|
+
# [+:ensuring_owner_was+]
|
|
1782
1782
|
# Specifies an instance method to be called on the owner. The method must return true in order for the
|
|
1783
1783
|
# associated records to be deleted in a background job.
|
|
1784
1784
|
#
|
|
@@ -1833,51 +1833,51 @@ module ActiveRecord
|
|
|
1833
1833
|
# +collection+ is a placeholder for the symbol passed as the +name+ argument, so
|
|
1834
1834
|
# <tt>has_and_belongs_to_many :categories</tt> would add among others <tt>categories.empty?</tt>.
|
|
1835
1835
|
#
|
|
1836
|
-
# [collection]
|
|
1836
|
+
# [<tt>collection</tt>]
|
|
1837
1837
|
# Returns a Relation of all the associated objects.
|
|
1838
1838
|
# An empty Relation is returned if none are found.
|
|
1839
|
-
# [collection<<(object, ...)]
|
|
1839
|
+
# [<tt>collection<<(object, ...)</tt>]
|
|
1840
1840
|
# Adds one or more objects to the collection by creating associations in the join table
|
|
1841
1841
|
# (<tt>collection.push</tt> and <tt>collection.concat</tt> are aliases to this method).
|
|
1842
1842
|
# Note that this operation instantly fires update SQL without waiting for the save or update call on the
|
|
1843
1843
|
# parent object, unless the parent object is a new record.
|
|
1844
|
-
# [collection.delete(object, ...)]
|
|
1844
|
+
# [<tt>collection.delete(object, ...)</tt>]
|
|
1845
1845
|
# Removes one or more objects from the collection by removing their associations from the join table.
|
|
1846
1846
|
# This does not destroy the objects.
|
|
1847
|
-
# [collection.destroy(object, ...)]
|
|
1847
|
+
# [<tt>collection.destroy(object, ...)</tt>]
|
|
1848
1848
|
# Removes one or more objects from the collection by running destroy on each association in the join table, overriding any dependent option.
|
|
1849
1849
|
# This does not destroy the objects.
|
|
1850
|
-
# [collection=objects]
|
|
1850
|
+
# [<tt>collection=objects</tt>]
|
|
1851
1851
|
# Replaces the collection's content by deleting and adding objects as appropriate.
|
|
1852
|
-
# [collection_singular_ids]
|
|
1852
|
+
# [<tt>collection_singular_ids</tt>]
|
|
1853
1853
|
# Returns an array of the associated objects' ids.
|
|
1854
|
-
# [collection_singular_ids=ids]
|
|
1854
|
+
# [<tt>collection_singular_ids=ids</tt>]
|
|
1855
1855
|
# Replace the collection by the objects identified by the primary keys in +ids+.
|
|
1856
|
-
# [collection.clear]
|
|
1856
|
+
# [<tt>collection.clear</tt>]
|
|
1857
1857
|
# Removes every object from the collection. This does not destroy the objects.
|
|
1858
|
-
# [collection.empty
|
|
1858
|
+
# [<tt>collection.empty?</tt>]
|
|
1859
1859
|
# Returns +true+ if there are no associated objects.
|
|
1860
|
-
# [collection.size]
|
|
1860
|
+
# [<tt>collection.size</tt>]
|
|
1861
1861
|
# Returns the number of associated objects.
|
|
1862
|
-
# [collection.find(id)]
|
|
1862
|
+
# [<tt>collection.find(id)</tt>]
|
|
1863
1863
|
# Finds an associated object responding to the +id+ and that
|
|
1864
1864
|
# meets the condition that it has to be associated with this object.
|
|
1865
1865
|
# Uses the same rules as ActiveRecord::FinderMethods#find.
|
|
1866
|
-
# [collection.exists?(...)]
|
|
1866
|
+
# [<tt>collection.exists?(...)</tt>]
|
|
1867
1867
|
# Checks whether an associated object with the given conditions exists.
|
|
1868
1868
|
# Uses the same rules as ActiveRecord::FinderMethods#exists?.
|
|
1869
|
-
# [collection.build(attributes = {})]
|
|
1869
|
+
# [<tt>collection.build(attributes = {})</tt>]
|
|
1870
1870
|
# Returns a new object of the collection type that has been instantiated
|
|
1871
1871
|
# with +attributes+ and linked to this object through the join table, but has not yet been saved.
|
|
1872
|
-
# [collection.create(attributes = {})]
|
|
1872
|
+
# [<tt>collection.create(attributes = {})</tt>]
|
|
1873
1873
|
# Returns a new object of the collection type that has been instantiated
|
|
1874
1874
|
# with +attributes+, linked to this object through the join table, and that has already been
|
|
1875
1875
|
# saved (if it passed the validation).
|
|
1876
|
-
# [collection.reload]
|
|
1876
|
+
# [<tt>collection.reload</tt>]
|
|
1877
1877
|
# Returns a Relation of all of the associated objects, forcing a database read.
|
|
1878
1878
|
# An empty Relation is returned if none are found.
|
|
1879
1879
|
#
|
|
1880
|
-
#
|
|
1880
|
+
# ==== Example
|
|
1881
1881
|
#
|
|
1882
1882
|
# A Developer class declares <tt>has_and_belongs_to_many :projects</tt>, which will add:
|
|
1883
1883
|
# * <tt>Developer#projects</tt>
|
|
@@ -1897,7 +1897,7 @@ module ActiveRecord
|
|
|
1897
1897
|
# * <tt>Developer#projects.reload</tt>
|
|
1898
1898
|
# The declaration may include an +options+ hash to specialize the behavior of the association.
|
|
1899
1899
|
#
|
|
1900
|
-
#
|
|
1900
|
+
# ==== Scopes
|
|
1901
1901
|
#
|
|
1902
1902
|
# You can pass a second argument +scope+ as a callable (i.e. proc or
|
|
1903
1903
|
# lambda) to retrieve a specific set of records or customize the generated
|
|
@@ -1909,7 +1909,7 @@ module ActiveRecord
|
|
|
1909
1909
|
# where("default_category = ?", post.default_category)
|
|
1910
1910
|
# }
|
|
1911
1911
|
#
|
|
1912
|
-
#
|
|
1912
|
+
# ==== Extensions
|
|
1913
1913
|
#
|
|
1914
1914
|
# The +extension+ argument allows you to pass a block into a
|
|
1915
1915
|
# has_and_belongs_to_many association. This is useful for adding new
|
|
@@ -1924,17 +1924,17 @@ module ActiveRecord
|
|
|
1924
1924
|
# end
|
|
1925
1925
|
# end
|
|
1926
1926
|
#
|
|
1927
|
-
#
|
|
1927
|
+
# ==== Options
|
|
1928
1928
|
#
|
|
1929
|
-
# [
|
|
1929
|
+
# [+:class_name+]
|
|
1930
1930
|
# Specify the class name of the association. Use it only if that name can't be inferred
|
|
1931
1931
|
# from the association name. So <tt>has_and_belongs_to_many :projects</tt> will by default be linked to the
|
|
1932
1932
|
# Project class, but if the real class name is SuperProject, you'll have to specify it with this option.
|
|
1933
|
-
# [
|
|
1933
|
+
# [+:join_table+]
|
|
1934
1934
|
# Specify the name of the join table if the default based on lexical order isn't what you want.
|
|
1935
1935
|
# <b>WARNING:</b> If you're overwriting the table name of either class, the +table_name+ method
|
|
1936
1936
|
# MUST be declared underneath any #has_and_belongs_to_many declaration in order to work.
|
|
1937
|
-
# [
|
|
1937
|
+
# [+:foreign_key+]
|
|
1938
1938
|
# Specify the foreign key used for the association. By default this is guessed to be the name
|
|
1939
1939
|
# of this class in lower-case and "_id" suffixed. So a Person class that makes
|
|
1940
1940
|
# a #has_and_belongs_to_many association to Project will use "person_id" as the
|
|
@@ -1942,15 +1942,15 @@ module ActiveRecord
|
|
|
1942
1942
|
#
|
|
1943
1943
|
# If you are going to modify the association (rather than just read from it), then it is
|
|
1944
1944
|
# a good idea to set the <tt>:inverse_of</tt> option.
|
|
1945
|
-
# [
|
|
1945
|
+
# [+:association_foreign_key+]
|
|
1946
1946
|
# Specify the foreign key used for the association on the receiving side of the association.
|
|
1947
1947
|
# By default this is guessed to be the name of the associated class in lower-case and "_id" suffixed.
|
|
1948
1948
|
# So if a Person class makes a #has_and_belongs_to_many association to Project,
|
|
1949
1949
|
# the association will use "project_id" as the default <tt>:association_foreign_key</tt>.
|
|
1950
|
-
# [
|
|
1950
|
+
# [+:validate+]
|
|
1951
1951
|
# When set to +true+, validates new objects added to association when saving the parent object. +true+ by default.
|
|
1952
1952
|
# If you want to ensure associated objects are revalidated on every update, use +validates_associated+.
|
|
1953
|
-
# [
|
|
1953
|
+
# [+:autosave+]
|
|
1954
1954
|
# If true, always save the associated objects or destroy them if marked for destruction, when
|
|
1955
1955
|
# saving the parent object.
|
|
1956
1956
|
# If false, never save or destroy the associated objects.
|
|
@@ -1958,7 +1958,7 @@ module ActiveRecord
|
|
|
1958
1958
|
#
|
|
1959
1959
|
# Note that NestedAttributes::ClassMethods#accepts_nested_attributes_for sets
|
|
1960
1960
|
# <tt>:autosave</tt> to <tt>true</tt>.
|
|
1961
|
-
# [
|
|
1961
|
+
# [+:strict_loading+]
|
|
1962
1962
|
# Enforces strict loading every time an associated record is loaded through this association.
|
|
1963
1963
|
#
|
|
1964
1964
|
# Option examples:
|