aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2019-04-25 11:45:16 +0900
committerGitHub <noreply@github.com>2019-04-25 11:45:16 +0900
commit6f8db3d1f31b9e1fa39677fd6a4b4d60c1b8db5b (patch)
treecc84a183f8f8fec81decd3558e190a10f054c527
parentd4d145a6795ee7f461ef86a07e73a1f13fdb8574 (diff)
parent71b56644322cf71d693b634287f652aa96d9750d (diff)
downloadrails-6f8db3d1f31b9e1fa39677fd6a4b4d60c1b8db5b.tar.gz
rails-6f8db3d1f31b9e1fa39677fd6a4b4d60c1b8db5b.tar.bz2
rails-6f8db3d1f31b9e1fa39677fd6a4b4d60c1b8db5b.zip
Merge pull request #36086 from abhaynikam/35869-add-documentation-for-has-one-option
Adds documentation for has_one touch option after #35869 [ci skip]
-rw-r--r--guides/source/association_basics.md27
1 files changed, 25 insertions, 2 deletions
diff --git a/guides/source/association_basics.md b/guides/source/association_basics.md
index a60ce7fb32..62e9270539 100644
--- a/guides/source/association_basics.md
+++ b/guides/source/association_basics.md
@@ -1231,6 +1231,7 @@ The `has_one` association supports these options:
* `:source`
* `:source_type`
* `:through`
+* `:touch`
* `:validate`
##### `:as`
@@ -1324,6 +1325,28 @@ class DustJacket < ApplicationRecord; end
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).
+##### `:touch`
+
+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:
+
+```ruby
+class Supplier < ApplicationRecord
+ has_one :account, touch: true
+end
+
+class Account < ApplicationRecord
+ belongs_to :supplier
+end
+```
+
+In this case, saving or destroying a supplier will update the timestamp on the associated account. You can also specify a particular timestamp attribute to update:
+
+```ruby
+class Supplier < ApplicationRecord
+ has_one :account, touch: :suppliers_updated_at
+end
+```
+
##### `:validate`
If you set the `:validate` option to `true`, then associated objects will be validated whenever you save this object. By default, this is `false`: associated objects will not be validated when this object is saved.
@@ -2383,11 +2406,11 @@ NOTE: These callbacks are called only when the associated objects are added or r
```ruby
# Triggers `before_add` callback
-author.books << book
+author.books << book
author.books = [book, book2]
# Does not trigger the `before_add` callback
-book.update(author_id: 1)
+book.update(author_id: 1)
```
### Association Extensions