1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #36086 from abhaynikam/35869-add-documentation-for-has-one-option

Adds documentation for has_one touch option after #35869 [ci skip]
This commit is contained in:
Ryuta Kamizono 2019-04-25 11:45:16 +09:00 committed by GitHub
commit 6f8db3d1f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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