diff options
author | Abhay Nikam <nikam.abhay1@gmail.com> | 2019-04-25 07:54:30 +0530 |
---|---|---|
committer | Abhay Nikam <nikam.abhay1@gmail.com> | 2019-04-25 08:06:16 +0530 |
commit | 71b56644322cf71d693b634287f652aa96d9750d (patch) | |
tree | cc84a183f8f8fec81decd3558e190a10f054c527 /guides | |
parent | d4d145a6795ee7f461ef86a07e73a1f13fdb8574 (diff) | |
download | rails-71b56644322cf71d693b634287f652aa96d9750d.tar.gz rails-71b56644322cf71d693b634287f652aa96d9750d.tar.bz2 rails-71b56644322cf71d693b634287f652aa96d9750d.zip |
Adds documentation for has_one touch option after #35869 [ci skip]
Diffstat (limited to 'guides')
-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 |