mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
[ci skip] setting :inverse_of works with :as, :through and :polymorphic options, and is needed for bi-directionality with a scope
[ci skip] Remove :conditions opion from association basics guide This got replaced by scopes.
This commit is contained in:
parent
5a593486f5
commit
4ab4364a51
2 changed files with 27 additions and 29 deletions
|
@ -1061,12 +1061,6 @@ module ActiveRecord
|
||||||
# belongs_to :dungeon, inverse_of: :evil_wizard
|
# belongs_to :dungeon, inverse_of: :evil_wizard
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# There are limitations to <tt>:inverse_of</tt> support:
|
|
||||||
#
|
|
||||||
# * does not work with <tt>:through</tt> associations.
|
|
||||||
# * does not work with <tt>:polymorphic</tt> associations.
|
|
||||||
# * inverse associations for #belongs_to associations #has_many are ignored.
|
|
||||||
#
|
|
||||||
# For more information, see the documentation for the +:inverse_of+ option.
|
# For more information, see the documentation for the +:inverse_of+ option.
|
||||||
#
|
#
|
||||||
# == Deleting from associations
|
# == Deleting from associations
|
||||||
|
@ -1279,6 +1273,9 @@ module ActiveRecord
|
||||||
# Specify the foreign key used for the association. By default this is guessed to be the name
|
# Specify the foreign key used for the association. By default this is guessed to be the name
|
||||||
# of this class in lower-case and "_id" suffixed. So a Person class that makes a #has_many
|
# of this class in lower-case and "_id" suffixed. So a Person class that makes a #has_many
|
||||||
# association will use "person_id" as the default <tt>:foreign_key</tt>.
|
# association will use "person_id" as the default <tt>:foreign_key</tt>.
|
||||||
|
#
|
||||||
|
# If you are going to modify the association (rather than just read from it), then it is
|
||||||
|
# a good idea to set the <tt>:inverse_of</tt> option.
|
||||||
# [:foreign_type]
|
# [:foreign_type]
|
||||||
# Specify the column used to store the associated object's type, if this is a polymorphic
|
# Specify the column used to store the associated object's type, if this is a polymorphic
|
||||||
# association. By default this is guessed to be the name of the polymorphic association
|
# association. By default this is guessed to be the name of the polymorphic association
|
||||||
|
@ -1352,8 +1349,7 @@ module ActiveRecord
|
||||||
# <tt>:autosave</tt> to <tt>true</tt>.
|
# <tt>:autosave</tt> to <tt>true</tt>.
|
||||||
# [:inverse_of]
|
# [:inverse_of]
|
||||||
# Specifies the name of the #belongs_to association on the associated object
|
# Specifies the name of the #belongs_to association on the associated object
|
||||||
# that is the inverse of this #has_many association. Does not work in combination
|
# that is the inverse of this #has_many association.
|
||||||
# with <tt>:through</tt> or <tt>:as</tt> options.
|
|
||||||
# See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail.
|
# See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail.
|
||||||
# [:extend]
|
# [:extend]
|
||||||
# Specifies a module or array of modules that will be extended into the association object returned.
|
# Specifies a module or array of modules that will be extended into the association object returned.
|
||||||
|
@ -1449,6 +1445,9 @@ module ActiveRecord
|
||||||
# Specify the foreign key used for the association. By default this is guessed to be the name
|
# Specify the foreign key used for the association. By default this is guessed to be the name
|
||||||
# of this class in lower-case and "_id" suffixed. So a Person class that makes a #has_one association
|
# of this class in lower-case and "_id" suffixed. So a Person class that makes a #has_one association
|
||||||
# will use "person_id" as the default <tt>:foreign_key</tt>.
|
# will use "person_id" as the default <tt>:foreign_key</tt>.
|
||||||
|
#
|
||||||
|
# If you are going to modify the association (rather than just read from it), then it is
|
||||||
|
# a good idea to set the <tt>:inverse_of</tt> option.
|
||||||
# [:foreign_type]
|
# [:foreign_type]
|
||||||
# Specify the column used to store the associated object's type, if this is a polymorphic
|
# Specify the column used to store the associated object's type, if this is a polymorphic
|
||||||
# association. By default this is guessed to be the name of the polymorphic association
|
# association. By default this is guessed to be the name of the polymorphic association
|
||||||
|
@ -1464,6 +1463,9 @@ module ActiveRecord
|
||||||
# <tt>:primary_key</tt>, and <tt>:foreign_key</tt> are ignored, as the association uses the
|
# <tt>:primary_key</tt>, and <tt>:foreign_key</tt> are ignored, as the association uses the
|
||||||
# source reflection. You can only use a <tt>:through</tt> query through a #has_one
|
# source reflection. You can only use a <tt>:through</tt> query through a #has_one
|
||||||
# or #belongs_to association on the join model.
|
# or #belongs_to association on the join model.
|
||||||
|
#
|
||||||
|
# If you are going to modify the association (rather than just read from it), then it is
|
||||||
|
# a good idea to set the <tt>:inverse_of</tt> option.
|
||||||
# [:source]
|
# [:source]
|
||||||
# Specifies the source association name used by #has_one <tt>:through</tt> queries.
|
# Specifies the source association name used by #has_one <tt>:through</tt> queries.
|
||||||
# Only use it if the name cannot be inferred from the association.
|
# Only use it if the name cannot be inferred from the association.
|
||||||
|
@ -1484,8 +1486,7 @@ module ActiveRecord
|
||||||
# <tt>:autosave</tt> to <tt>true</tt>.
|
# <tt>:autosave</tt> to <tt>true</tt>.
|
||||||
# [:inverse_of]
|
# [:inverse_of]
|
||||||
# Specifies the name of the #belongs_to association on the associated object
|
# Specifies the name of the #belongs_to association on the associated object
|
||||||
# that is the inverse of this #has_one association. Does not work in combination
|
# that is the inverse of this #has_one association.
|
||||||
# with <tt>:through</tt> or <tt>:as</tt> options.
|
|
||||||
# See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail.
|
# See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail.
|
||||||
# [:required]
|
# [:required]
|
||||||
# When set to +true+, the association will also have its presence validated.
|
# When set to +true+, the association will also have its presence validated.
|
||||||
|
@ -1570,6 +1571,9 @@ module ActiveRecord
|
||||||
# association will use "person_id" as the default <tt>:foreign_key</tt>. Similarly,
|
# association will use "person_id" as the default <tt>:foreign_key</tt>. Similarly,
|
||||||
# <tt>belongs_to :favorite_person, class_name: "Person"</tt> will use a foreign key
|
# <tt>belongs_to :favorite_person, class_name: "Person"</tt> will use a foreign key
|
||||||
# of "favorite_person_id".
|
# of "favorite_person_id".
|
||||||
|
#
|
||||||
|
# If you are going to modify the association (rather than just read from it), then it is
|
||||||
|
# a good idea to set the <tt>:inverse_of</tt> option.
|
||||||
# [:foreign_type]
|
# [:foreign_type]
|
||||||
# Specify the column used to store the associated object's type, if this is a polymorphic
|
# Specify the column used to store the associated object's type, if this is a polymorphic
|
||||||
# association. By default this is guessed to be the name of the association with a "_type"
|
# association. By default this is guessed to be the name of the association with a "_type"
|
||||||
|
@ -1619,8 +1623,7 @@ module ActiveRecord
|
||||||
# +after_commit+ and +after_rollback+ callbacks are executed.
|
# +after_commit+ and +after_rollback+ callbacks are executed.
|
||||||
# [:inverse_of]
|
# [:inverse_of]
|
||||||
# Specifies the name of the #has_one or #has_many association on the associated
|
# Specifies the name of the #has_one or #has_many association on the associated
|
||||||
# object that is the inverse of this #belongs_to association. Does not work in
|
# object that is the inverse of this #belongs_to association.
|
||||||
# combination with the <tt>:polymorphic</tt> options.
|
|
||||||
# See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail.
|
# See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail.
|
||||||
# [:optional]
|
# [:optional]
|
||||||
# When set to +true+, the association will not have its presence validated.
|
# When set to +true+, the association will not have its presence validated.
|
||||||
|
@ -1789,6 +1792,9 @@ module ActiveRecord
|
||||||
# of this class in lower-case and "_id" suffixed. So a Person class that makes
|
# of this class in lower-case and "_id" suffixed. So a Person class that makes
|
||||||
# a #has_and_belongs_to_many association to Project will use "person_id" as the
|
# a #has_and_belongs_to_many association to Project will use "person_id" as the
|
||||||
# default <tt>:foreign_key</tt>.
|
# default <tt>:foreign_key</tt>.
|
||||||
|
#
|
||||||
|
# If you are going to modify the association (rather than just read from it), then it is
|
||||||
|
# a good idea to set the <tt>:inverse_of</tt> option.
|
||||||
# [:association_foreign_key]
|
# [:association_foreign_key]
|
||||||
# Specify the foreign key used for the association on the receiving side of the association.
|
# Specify the foreign key used for the association on the receiving side of the association.
|
||||||
# By default this is guessed to be the name of the associated class in lower-case and "_id" suffixed.
|
# By default this is guessed to be the name of the associated class in lower-case and "_id" suffixed.
|
||||||
|
|
|
@ -735,12 +735,9 @@ a.first_name = 'David'
|
||||||
a.first_name == b.author.first_name # => true
|
a.first_name == b.author.first_name # => true
|
||||||
```
|
```
|
||||||
|
|
||||||
Active Record supports automatic identification for most associations with standard names. However, Active Record will not automatically identify bi-directional associations that contain any of the following options:
|
Active Record supports automatic identification for most associations with standard names. However, Active Record will not automatically identify bi-directional associations that contain a scope or any of the following options:
|
||||||
|
|
||||||
* `:conditions`
|
|
||||||
* `:through`
|
* `:through`
|
||||||
* `:polymorphic`
|
|
||||||
* `:class_name`
|
|
||||||
* `:foreign_key`
|
* `:foreign_key`
|
||||||
|
|
||||||
For example, consider the following model declarations:
|
For example, consider the following model declarations:
|
||||||
|
@ -787,12 +784,6 @@ a.first_name = 'David'
|
||||||
a.first_name == b.writer.first_name # => true
|
a.first_name == b.writer.first_name # => true
|
||||||
```
|
```
|
||||||
|
|
||||||
There are a few limitations to `:inverse_of` support:
|
|
||||||
|
|
||||||
* They do not work with `:through` associations.
|
|
||||||
* They do not work with `:polymorphic` associations.
|
|
||||||
* They do not work with `:as` associations.
|
|
||||||
|
|
||||||
Detailed Association Reference
|
Detailed Association Reference
|
||||||
------------------------------
|
------------------------------
|
||||||
|
|
||||||
|
@ -1012,7 +1003,7 @@ When we execute `@user.todos.create` then the `@todo` record will have its
|
||||||
|
|
||||||
##### `:inverse_of`
|
##### `:inverse_of`
|
||||||
|
|
||||||
The `:inverse_of` option specifies the name of the `has_many` or `has_one` association that is the inverse of this association. Does not work in combination with the `:polymorphic` options.
|
The `:inverse_of` option specifies the name of the `has_many` or `has_one` association that is the inverse of this association.
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class Author < ApplicationRecord
|
class Author < ApplicationRecord
|
||||||
|
@ -1066,7 +1057,8 @@ There may be times when you wish to customize the query used by `belongs_to`. Su
|
||||||
```ruby
|
```ruby
|
||||||
class Book < ApplicationRecord
|
class Book < ApplicationRecord
|
||||||
belongs_to :author, -> { where active: true },
|
belongs_to :author, -> { where active: true },
|
||||||
dependent: :destroy
|
dependent: :destroy,
|
||||||
|
inverse_of: :books
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -1082,8 +1074,8 @@ You can use any of the standard [querying methods](active_record_querying.html)
|
||||||
The `where` method lets you specify the conditions that the associated object must meet.
|
The `where` method lets you specify the conditions that the associated object must meet.
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class book < ApplicationRecord
|
class Book < ApplicationRecord
|
||||||
belongs_to :author, -> { where active: true }
|
belongs_to :author, -> { where active: true }, inverse_of: :books
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -1299,7 +1291,7 @@ TIP: In any case, Rails will not create foreign key columns for you. You need to
|
||||||
|
|
||||||
##### `:inverse_of`
|
##### `:inverse_of`
|
||||||
|
|
||||||
The `:inverse_of` option specifies the name of the `belongs_to` association that is the inverse of this association. Does not work in combination with the `:through` or `:as` options.
|
The `:inverse_of` option specifies the name of the `belongs_to` association that is the inverse of this association.
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class Supplier < ApplicationRecord
|
class Supplier < ApplicationRecord
|
||||||
|
@ -1337,7 +1329,7 @@ There may be times when you wish to customize the query used by `has_one`. Such
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class Supplier < ApplicationRecord
|
class Supplier < ApplicationRecord
|
||||||
has_one :account, -> { where active: true }
|
has_one :account, -> { where active: true }, inverse_of: :supplier
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -1694,7 +1686,7 @@ TIP: In any case, Rails will not create foreign key columns for you. You need to
|
||||||
|
|
||||||
##### `:inverse_of`
|
##### `:inverse_of`
|
||||||
|
|
||||||
The `:inverse_of` option specifies the name of the `belongs_to` association that is the inverse of this association. Does not work in combination with the `:through` or `:as` options.
|
The `:inverse_of` option specifies the name of the `belongs_to` association that is the inverse of this association.
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class Author < ApplicationRecord
|
class Author < ApplicationRecord
|
||||||
|
|
Loading…
Reference in a new issue