You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When set to `true`, the association will not have its presence validated.
1212
+
1204
1213
##### `:dependent`
1205
1214
1206
1215
If you set the `:dependent` option to:
@@ -1217,6 +1226,10 @@ If you set the `:dependent` option to:
1217
1226
1218
1227
WARNING: You should not specify this option on a `belongs_to` association that is connected with a `has_many` association on the other class. Doing so can lead to orphaned records in your database.
1219
1228
1229
+
##### `:ensuring_owner_was`
1230
+
1231
+
Specifies an instance method to be called on the owner. The method must return true in order for the associated records to be deleted in a background job.
1232
+
1220
1233
##### `:foreign_key`
1221
1234
1222
1235
By convention, Rails assumes that the column used to hold the foreign key on this model is the name of the association with the suffix `_id` added. The `:foreign_key` option lets you set the name of the foreign key directly:
@@ -1230,6 +1243,10 @@ end
1230
1243
1231
1244
TIP: In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations.
1232
1245
1246
+
##### `:foreign_type`
1247
+
1248
+
Specify the column used to store the associated object’s type, if this is a polymorphic association. By default this is guessed to be the name of the association with a “`_type`” suffix. So a class that defines a `belongs_to :taggable, polymorphic: true` association will use “`taggable_type`” as the default `:foreign_type`.
1249
+
1233
1250
##### `:primary_key`
1234
1251
1235
1252
By convention, Rails assumes that the `id` column is used to hold the primary key
@@ -1265,10 +1282,26 @@ class Book < ApplicationRecord
1265
1282
end
1266
1283
```
1267
1284
1285
+
##### `:optional`
1286
+
1287
+
If you set the `:optional` option to `true`, then the presence of the associated
1288
+
object won't be validated. By default, this option is set to `false`.
1289
+
1268
1290
##### `:polymorphic`
1269
1291
1270
1292
Passing `true` to the `:polymorphic` option indicates that this is a polymorphic association. Polymorphic associations were discussed in detail <ahref="#polymorphic-associations">earlier in this guide</a>.
1271
1293
1294
+
1295
+
##### `:required`
1296
+
1297
+
When set to `true`, the association will also have its presence validated. This will validate the association itself, not the id. You can use `:inverse_of` to avoid an extra query during validation.
1298
+
1299
+
NOTE: required is set to `true` by default and is deprecated. If you don’t want to have association presence validated, use `optional: true`.
1300
+
1301
+
##### `:strict_loading`
1302
+
1303
+
Enforces strict loading every time the associated record is loaded through this association.
1304
+
1272
1305
##### `:touch`
1273
1306
1274
1307
If you set the `:touch` option to `true`, then the `updated_at` or `updated_on` timestamp on the associated object will be set to the current time whenever this object is saved or destroyed:
@@ -1295,11 +1328,6 @@ end
1295
1328
1296
1329
If you set the `:validate` option to `true`, then new associated objects will be validated whenever you save this object. By default, this is `false`: new associated objects will not be validated when this object is saved.
1297
1330
1298
-
##### `:optional`
1299
-
1300
-
If you set the `:optional` option to `true`, then the presence of the associated
1301
-
object won't be validated. By default, this option is set to `false`.
1302
-
1303
1331
#### Scopes for `belongs_to`
1304
1332
1305
1333
There may be times when you wish to customize the query used by `belongs_to`. Such customizations can be achieved via a scope block. For example:
@@ -1489,11 +1517,16 @@ The [`has_one`][] association supports these options:
1489
1517
*`:autosave`
1490
1518
*`:class_name`
1491
1519
*`:dependent`
1520
+
*`:disable_joins`
1521
+
*`:ensuring_owner_was`
1492
1522
*`:foreign_key`
1493
1523
*`:inverse_of`
1494
1524
*`:primary_key`
1525
+
*`:query_constraints`
1526
+
*`:required`
1495
1527
*`:source`
1496
1528
*`:source_type`
1529
+
*`:strict_loading`
1497
1530
*`:through`
1498
1531
*`:touch`
1499
1532
*`:validate`
@@ -1533,6 +1566,10 @@ destroy such associations you won't be able to change the associated object
1533
1566
because the initial associated object's foreign key will be set to the
1534
1567
unallowed `NULL` value.
1535
1568
1569
+
##### `:disable_joins`
1570
+
1571
+
Specifies whether joins should be skipped for an association. If set to `true`, two or more queries will be generated. Note that in some cases, if order or limit is applied, it will be done in-memory due to database limitations. This option is only applicable on `has_one :through` associations as `has_one` alone does not perform a join.
1572
+
1536
1573
##### `:foreign_key`
1537
1574
1538
1575
By convention, Rails assumes that the column used to hold the foreign key on the other model is the name of this model with the suffix `_id` added. The `:foreign_key` option lets you set the name of the foreign key directly:
@@ -1564,6 +1601,14 @@ end
1564
1601
1565
1602
By convention, Rails assumes that the column used to hold the primary key of this model is `id`. You can override this and explicitly specify the primary key with the `:primary_key` option.
1566
1603
1604
+
##### `:query_constraints`
1605
+
1606
+
Serves as a composite foreign key. Defines the list of columns to be used to query the associated object. This is an optional option. By default Rails will attempt to derive the value automatically. When the value is set the Array size must match associated model’s primary key or query_constraints size.
1607
+
1608
+
##### `:required`
1609
+
1610
+
When set to `true`, the association will also have its presence validated. This will validate the association itself, not the id. You can use `:inverse_of` to avoid an extra query during validation.
1611
+
1567
1612
##### `:source`
1568
1613
1569
1614
The `:source` option specifies the source association name for a `has_one :through` association.
@@ -1592,6 +1637,10 @@ end
1592
1637
classDustJacket < ApplicationRecord; end
1593
1638
```
1594
1639
1640
+
##### `:strict_loading`
1641
+
1642
+
Enforces strict loading every time the associated record is loaded through this association.
1643
+
1595
1644
##### `:through`
1596
1645
1597
1646
The `:through` option specifies a join model through which to perform the query. `has_one :through` associations were discussed in detail [earlier in this guide](#the-has-one-through-association).
@@ -1944,11 +1993,17 @@ The [`has_many`][] association supports these options:
1944
1993
*`:class_name`
1945
1994
*`:counter_cache`
1946
1995
*`:dependent`
1996
+
*`:disable_joins`
1997
+
*`:ensuring_owner_was`
1998
+
*`:extend`
1947
1999
*`:foreign_key`
2000
+
*`:foreign_type`
1948
2001
*`:inverse_of`
1949
2002
*`:primary_key`
2003
+
*`:query_constraints`
1950
2004
*`:source`
1951
2005
*`:source_type`
2006
+
*`:strict_loading`
1952
2007
*`:through`
1953
2008
*`:validate`
1954
2009
@@ -1987,6 +2042,18 @@ Controls what happens to the associated objects when their owner is destroyed:
1987
2042
1988
2043
The `:destroy` and `:delete_all` options also affect the semantics of the `collection.delete` and `collection=` methods by causing them to destroy associated objects when they are removed from the collection.
1989
2044
2045
+
##### `:disable_joins`
2046
+
2047
+
Specifies whether joins should be skipped for an association. If set to true, two or more queries will be generated. Note that in some cases, if order or limit is applied, it will be done in-memory due to database limitations. This option is only applicable on `has_many :through associations` as has_many alone do not perform a join.
2048
+
2049
+
##### `:ensuring_owner_was`
2050
+
2051
+
Specifies an instance method to be called on the owner. The method must return true in order for the associated records to be deleted in a background job.
2052
+
2053
+
##### `:extend`
2054
+
2055
+
Specifies a module or array of modules that will be extended into the association object returned. Useful for defining methods on associations, especially when they should be shared between multiple association objects.
2056
+
1990
2057
##### `:foreign_key`
1991
2058
1992
2059
By convention, Rails assumes that the column used to hold the foreign key on the other model is the name of this model with the suffix `_id` added. The `:foreign_key` option lets you set the name of the foreign key directly:
@@ -1999,6 +2066,10 @@ end
1999
2066
2000
2067
TIP: In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations.
2001
2068
2069
+
##### `:foreign_type`
2070
+
2071
+
Specify the column used to store the associated object’s type, if this is a polymorphic association. By default this is guessed to be the name of the polymorphic association specified on “`as`” option with a “`_type`” suffix. So a class that defines a `has_many :tags, as: :taggable` association will use “`taggable_type`” as the default `:foreign_type`.
2072
+
2002
2073
##### `:inverse_of`
2003
2074
2004
2075
The `:inverse_of` option specifies the name of the `belongs_to` association that is the inverse of this association.
@@ -2032,6 +2103,10 @@ end
2032
2103
Now if we execute `@todo = @user.todos.create` then the `@todo`
2033
2104
record's `user_id` value will be the `guid` value of `@user`.
2034
2105
2106
+
##### `:query_constraints`
2107
+
2108
+
Serves as a composite foreign key. Defines the list of columns to be used to query the associated object. This is an optional option. By default Rails will attempt to derive the value automatically. When the value is set the Array size must match associated model’s primary key or `query_constraints` size.
2109
+
2035
2110
##### `:source`
2036
2111
2037
2112
The `:source` option specifies the source association name for a `has_many :through` association. You only need to use this option if the name of the source association cannot be automatically inferred from the association name.
@@ -2054,6 +2129,10 @@ class Hardback < ApplicationRecord; end
2054
2129
classPaperback < ApplicationRecord; end
2055
2130
```
2056
2131
2132
+
##### `:strict_loading`
2133
+
2134
+
When set to true, enforces strict loading every time the associated record is loaded through this association.
2135
+
2057
2136
##### `:through`
2058
2137
2059
2138
The `:through` option specifies a join model through which to perform the query. `has_many :through` associations provide a way to implement many-to-many relationships, as discussed [earlier in this guide](#the-has-many-through-association).
@@ -2486,6 +2565,7 @@ The [`has_and_belongs_to_many`][] association supports these options:
2486
2565
*`:class_name`
2487
2566
*`:foreign_key`
2488
2567
*`:join_table`
2568
+
*`:strict_loading`
2489
2569
*`:validate`
2490
2570
2491
2571
##### `:association_foreign_key`
@@ -2534,6 +2614,10 @@ end
2534
2614
2535
2615
If the default name of the join table, based on lexical ordering, is not what you want, you can use the `:join_table` option to override the default.
2536
2616
2617
+
##### `:strict_loading`
2618
+
2619
+
Enforces strict loading every time an associated record is loaded through this association.
2620
+
2537
2621
##### `:validate`
2538
2622
2539
2623
If you set the `:validate` option to `false`, then new associated objects will not be validated whenever you save this object. By default, this is `true`: new associated objects will be validated when this object is saved.
0 commit comments