aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations.rb
diff options
context:
space:
mode:
authorRoque Pinel <repinel@gmail.com>2015-06-13 09:37:55 -0400
committerRoque Pinel <repinel@gmail.com>2015-06-13 09:37:55 -0400
commit55703f94c094fd4cc13268e3720394eb4f3cb7a5 (patch)
treee981656f6534f928ff9cd3b91dd5335a0ddbb4ba /activerecord/lib/active_record/associations.rb
parent335a2b8a40d6f1553a76328fd89fc90a93d8d234 (diff)
downloadrails-55703f94c094fd4cc13268e3720394eb4f3cb7a5.tar.gz
rails-55703f94c094fd4cc13268e3720394eb4f3cb7a5.tar.bz2
rails-55703f94c094fd4cc13268e3720394eb4f3cb7a5.zip
[ci skip] Fix the API docs for Bi-directional associations
The examples now take in consideration when Active Record finds inverse associations automatically.
Diffstat (limited to 'activerecord/lib/active_record/associations.rb')
-rw-r--r--activerecord/lib/active_record/associations.rb19
1 files changed, 7 insertions, 12 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 1ca648d48d..82cb3fed59 100644
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -955,20 +955,16 @@ module ActiveRecord
# The +traps+ association on +Dungeon+ and the +dungeon+ association on +Trap+ are
# the inverse of each other and the inverse of the +dungeon+ association on +EvilWizard+
# is the +evil_wizard+ association on +Dungeon+ (and vice-versa). By default,
- # Active Record doesn't know anything about these inverse relationships and so no object
- # loading optimization is possible. For example:
+ # Active Record can guess the inverse of the association based on the name
+ # of the class. The result is the following:
#
# d = Dungeon.first
# t = d.traps.first
- # d.level == t.dungeon.level # => true
- # d.level = 10
- # d.level == t.dungeon.level # => false
+ # d.object_id == t.dungeon.object_id # => true
#
# The +Dungeon+ instances +d+ and <tt>t.dungeon</tt> in the above example refer to
- # the same object data from the database, but are actually different in-memory copies
- # of that data. Specifying the <tt>:inverse_of</tt> option on associations lets you tell
- # Active Record about inverse relationships and it will optimise object loading. For
- # example, if we changed our model definitions to:
+ # the same in-memory instance since the association matches the name of the class.
+ # The result would be the same if we added +:inverse_of+ to our model definitions:
#
# class Dungeon < ActiveRecord::Base
# has_many :traps, inverse_of: :dungeon
@@ -983,15 +979,14 @@ module ActiveRecord
# belongs_to :dungeon, inverse_of: :evil_wizard
# end
#
- # Then, from our code snippet above, +d+ and <tt>t.dungeon</tt> are actually the same
- # in-memory instance and our final <tt>d.level == t.dungeon.level</tt> will return +true+.
- #
# 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.
# * for +belongs_to+ associations +has_many+ inverse associations are ignored.
#
+ # For more information, see the documentation for the +:inverse_of+ option.
+ #
# == Deleting from associations
#
# === Dependent associations