mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
[skip ci] Add examples for has_{one,many} :through :source and :source_type (#35612)
* Add example for has_many :through source/source_type * Add example for has_one :through source/source_type
This commit is contained in:
parent
b1fc1319df
commit
af56c5c1c6
1 changed files with 29 additions and 0 deletions
|
@ -1305,6 +1305,21 @@ The `:source` option specifies the source association name for a `has_one :throu
|
||||||
|
|
||||||
The `:source_type` option specifies the source association type for a `has_one :through` association that proceeds through a polymorphic association.
|
The `:source_type` option specifies the source association type for a `has_one :through` association that proceeds through a polymorphic association.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
class Book < ApplicationRecord
|
||||||
|
has_one :format, polymorphic: true
|
||||||
|
has_one :dust_jacket, through: :format, source: :dust_jacket, source_type: "Hardback"
|
||||||
|
end
|
||||||
|
|
||||||
|
class Paperback < ApplicationRecord; end
|
||||||
|
|
||||||
|
class Hardback < ApplicationRecord
|
||||||
|
has_one :dust_jacket
|
||||||
|
end
|
||||||
|
|
||||||
|
class DustJacket < ApplicationRecord; end
|
||||||
|
```
|
||||||
|
|
||||||
##### `:through`
|
##### `:through`
|
||||||
|
|
||||||
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).
|
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).
|
||||||
|
@ -1717,6 +1732,20 @@ The `:source` option specifies the source association name for a `has_many :thro
|
||||||
|
|
||||||
The `:source_type` option specifies the source association type for a `has_many :through` association that proceeds through a polymorphic association.
|
The `:source_type` option specifies the source association type for a `has_many :through` association that proceeds through a polymorphic association.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
class Author < ApplicationRecord
|
||||||
|
has_many :books
|
||||||
|
has_many :paperbacks, through: :books, source: :format, source_type: "Paperback"
|
||||||
|
end
|
||||||
|
|
||||||
|
class Book < ApplicationRecord
|
||||||
|
has_one :format, polymorphic: true
|
||||||
|
end
|
||||||
|
|
||||||
|
class Hardback < ApplicationRecord; end
|
||||||
|
class Paperback < ApplicationRecord; end
|
||||||
|
```
|
||||||
|
|
||||||
##### `:through`
|
##### `:through`
|
||||||
|
|
||||||
The `:through` option specifies a join model through which to perform the query. `has_many :through` associations provide a way to implement many-to-many relationships, as discussed [earlier in this guide](#the-has-many-through-association).
|
The `:through` option specifies a join model through which to perform the query. `has_many :through` associations provide a way to implement many-to-many relationships, as discussed [earlier in this guide](#the-has-many-through-association).
|
||||||
|
|
Loading…
Reference in a new issue