diff options
Diffstat (limited to 'guides/source/association_basics.md')
-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 |