From 4ab4364a51e7e95c15eaa09fc6b7234afcfbc6f4 Mon Sep 17 00:00:00 2001 From: Bart de Water Date: Thu, 14 Dec 2017 08:22:25 -0500 Subject: [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. --- guides/source/association_basics.md | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) (limited to 'guides') diff --git a/guides/source/association_basics.md b/guides/source/association_basics.md index b5e236b790..fa522f2eaf 100644 --- a/guides/source/association_basics.md +++ b/guides/source/association_basics.md @@ -735,12 +735,9 @@ a.first_name = 'David' 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` -* `:polymorphic` -* `:class_name` * `:foreign_key` For example, consider the following model declarations: @@ -787,12 +784,6 @@ a.first_name = 'David' 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 ------------------------------ @@ -1012,7 +1003,7 @@ When we execute `@user.todos.create` then the `@todo` record will have its ##### `: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 class Author < ApplicationRecord @@ -1066,7 +1057,8 @@ There may be times when you wish to customize the query used by `belongs_to`. Su ```ruby class Book < ApplicationRecord belongs_to :author, -> { where active: true }, - dependent: :destroy + dependent: :destroy, + inverse_of: :books 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. ```ruby -class book < ApplicationRecord - belongs_to :author, -> { where active: true } +class Book < ApplicationRecord + belongs_to :author, -> { where active: true }, inverse_of: :books end ``` @@ -1299,7 +1291,7 @@ TIP: In any case, Rails will not create foreign key columns for you. You need to ##### `: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 class Supplier < ApplicationRecord @@ -1337,7 +1329,7 @@ There may be times when you wish to customize the query used by `has_one`. Such ```ruby class Supplier < ApplicationRecord - has_one :account, -> { where active: true } + has_one :account, -> { where active: true }, inverse_of: :supplier end ``` @@ -1694,7 +1686,7 @@ TIP: In any case, Rails will not create foreign key columns for you. You need to ##### `: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 class Author < ApplicationRecord -- cgit v1.2.3