Skip to content

Commit a5f113f

Browse files
authored
Merge pull request rails#49423 from paulreece/update_has_one_doc
Added available methods for has_one association to guide.
2 parents 180e601 + 5195581 commit a5f113f

File tree

1 file changed

+90
-6
lines changed

1 file changed

+90
-6
lines changed

guides/source/association_basics.md

Lines changed: 90 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,14 +1118,19 @@ The [`belongs_to`][] association supports these options:
11181118
* `:autosave`
11191119
* `:class_name`
11201120
* `:counter_cache`
1121+
* `:default`
11211122
* `:dependent`
1123+
* `:ensuring_owner_was`
11221124
* `:foreign_key`
1125+
* `:foreign_type`
11231126
* `:primary_key`
11241127
* `:inverse_of`
1128+
* `:optional`
11251129
* `:polymorphic`
1130+
* `:required`
1131+
* `:strict_loading`
11261132
* `:touch`
11271133
* `:validate`
1128-
* `:optional`
11291134

11301135
##### `:autosave`
11311136

@@ -1201,6 +1206,10 @@ towards the counter. To fix a stale counter cache, use [`reset_counters`][].
12011206

12021207
[`reset_counters`]: https://api.rubyonrails.org/classes/ActiveRecord/CounterCache/ClassMethods.html#method-i-reset_counters
12031208

1209+
##### `:default`
1210+
1211+
When set to `true`, the association will not have its presence validated.
1212+
12041213
##### `:dependent`
12051214

12061215
If you set the `:dependent` option to:
@@ -1217,6 +1226,10 @@ If you set the `:dependent` option to:
12171226

12181227
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.
12191228

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+
12201233
##### `:foreign_key`
12211234

12221235
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
12301243

12311244
TIP: In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations.
12321245

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+
12331250
##### `:primary_key`
12341251

12351252
By convention, Rails assumes that the `id` column is used to hold the primary key
@@ -1265,10 +1282,26 @@ class Book < ApplicationRecord
12651282
end
12661283
```
12671284

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+
12681290
##### `:polymorphic`
12691291

12701292
Passing `true` to the `:polymorphic` option indicates that this is a polymorphic association. Polymorphic associations were discussed in detail <a href="#polymorphic-associations">earlier in this guide</a>.
12711293

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+
12721305
##### `:touch`
12731306

12741307
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
12951328

12961329
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.
12971330

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-
13031331
#### Scopes for `belongs_to`
13041332

13051333
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:
14891517
* `:autosave`
14901518
* `:class_name`
14911519
* `:dependent`
1520+
* `:disable_joins`
1521+
* `:ensuring_owner_was`
14921522
* `:foreign_key`
14931523
* `:inverse_of`
14941524
* `:primary_key`
1525+
* `:query_constraints`
1526+
* `:required`
14951527
* `:source`
14961528
* `:source_type`
1529+
* `:strict_loading`
14971530
* `:through`
14981531
* `:touch`
14991532
* `:validate`
@@ -1533,6 +1566,10 @@ destroy such associations you won't be able to change the associated object
15331566
because the initial associated object's foreign key will be set to the
15341567
unallowed `NULL` value.
15351568

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+
15361573
##### `:foreign_key`
15371574

15381575
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
15641601

15651602
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.
15661603

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+
15671612
##### `:source`
15681613

15691614
The `:source` option specifies the source association name for a `has_one :through` association.
@@ -1592,6 +1637,10 @@ end
15921637
class DustJacket < ApplicationRecord; end
15931638
```
15941639

1640+
##### `:strict_loading`
1641+
1642+
Enforces strict loading every time the associated record is loaded through this association.
1643+
15951644
##### `:through`
15961645

15971646
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:
19441993
* `:class_name`
19451994
* `:counter_cache`
19461995
* `:dependent`
1996+
* `:disable_joins`
1997+
* `:ensuring_owner_was`
1998+
* `:extend`
19471999
* `:foreign_key`
2000+
* `:foreign_type`
19482001
* `:inverse_of`
19492002
* `:primary_key`
2003+
* `:query_constraints`
19502004
* `:source`
19512005
* `:source_type`
2006+
* `:strict_loading`
19522007
* `:through`
19532008
* `:validate`
19542009

@@ -1987,6 +2042,18 @@ Controls what happens to the associated objects when their owner is destroyed:
19872042

19882043
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.
19892044

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+
19902057
##### `:foreign_key`
19912058

19922059
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
19992066

20002067
TIP: In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations.
20012068

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+
20022073
##### `:inverse_of`
20032074

20042075
The `:inverse_of` option specifies the name of the `belongs_to` association that is the inverse of this association.
@@ -2032,6 +2103,10 @@ end
20322103
Now if we execute `@todo = @user.todos.create` then the `@todo`
20332104
record's `user_id` value will be the `guid` value of `@user`.
20342105

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+
20352110
##### `:source`
20362111

20372112
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
20542129
class Paperback < ApplicationRecord; end
20552130
```
20562131

2132+
##### `:strict_loading`
2133+
2134+
When set to true, enforces strict loading every time the associated record is loaded through this association.
2135+
20572136
##### `:through`
20582137

20592138
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:
24862565
* `:class_name`
24872566
* `:foreign_key`
24882567
* `:join_table`
2568+
* `:strict_loading`
24892569
* `:validate`
24902570

24912571
##### `:association_foreign_key`
@@ -2534,6 +2614,10 @@ end
25342614

25352615
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.
25362616

2617+
##### `:strict_loading`
2618+
2619+
Enforces strict loading every time an associated record is loaded through this association.
2620+
25372621
##### `:validate`
25382622

25392623
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

Comments
 (0)