diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2019-04-25 11:45:16 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-25 11:45:16 +0900 |
commit | 6f8db3d1f31b9e1fa39677fd6a4b4d60c1b8db5b (patch) | |
tree | cc84a183f8f8fec81decd3558e190a10f054c527 /guides/source | |
parent | d4d145a6795ee7f461ef86a07e73a1f13fdb8574 (diff) | |
parent | 71b56644322cf71d693b634287f652aa96d9750d (diff) | |
download | rails-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]
Diffstat (limited to 'guides/source')
-rw-r--r-- | guides/source/association_basics.md | 27 |
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 |