@@ -1211,9 +1211,10 @@ that a bundle may contain other bundles or products:
12111211 has_many :items, polymorphic: true
12121212 end
12131213
1214- Starting in version 9.0.2, Mongoid adds support to custom polymorphic types via
1215- a global registry. You can now specify alternative keys to represent different
1216- classes, thus decoupling the code from the data. For example:
1214+ Starting in version 9.0.2, Mongoid adds support to custom polymorphic types through
1215+ a global registry. You can specify alternative keys to represent different
1216+ classes, thus decoupling the code from the data. The following example specifies
1217+ the string ``"dept"`` as an alternate key for the ``Department`` class:
12171218
12181219.. code-block:: ruby
12191220
@@ -1223,16 +1224,18 @@ classes, thus decoupling the code from the data. For example:
12231224 has_many :managers, as: :unit
12241225 end
12251226
1226- The ``identify_as 'dept'`` directive says that this class should be identified by
1227- the string "dept" in the database. You can specify multiple aliases, too (e.g.
1228- ``entify_as 'dept', 'div', 'agency'``), in which case the first key is the "default",
1229- and the others are only used for looking up records. This lets you refactor your
1230- code without breaking the associations in your data.
1227+ The ``identify_as 'dept'`` directive instructs Mongoid to store this class in the
1228+ database as the string ``"dept"``. You can also specify multiple aliases. For
1229+ example, you can specify the option as: ``identify_as 'dept', 'div', 'agency'``,
1230+ in which case the first key is the "default", and the others are used only for
1231+ looking up records. This lets you refactor your code without breaking the associations
1232+ in your data.
12311233
1232- Note that these aliases are global; the keys you specify must be unique across your
1234+ These aliases are global. The keys you specify must be unique across your
12331235entire code base. However, it is possible to register alternative resolvers, which
12341236must be used for different subsets of your models. In this case, the keys must
1235- only be unique for each resolver. For example:
1237+ only be unique for each resolver. The following example shows how to register
1238+ alternate resolvers:
12361239
12371240.. code-block:: ruby
12381241
@@ -1245,26 +1248,15 @@ only be unique for each resolver. For example:
12451248 identify_as 'dept', resolver: :eng
12461249 end
12471250
1248- class Manager
1249- include Mongoid::Document
1250- belongs_to :unit, polymorphic: :eng
1251- end
1252- end
1253-
12541251 module Purchasing
12551252 class Department
12561253 include Mongoid::Document
12571254 identify_as 'dept', resolver: :purch
12581255 end
12591256
1260- class Manager
1261- include Mongoid::Document
1262- belongs_to :unit, polymorphic: :purch
1263- end
1264- end
12651257
12661258Both ``Engineering::Department`` and ``Purchasing::Department`` are aliased as
1267- ``"dept"``, but use their own resolver, so there is no conflict.
1259+ ``"dept"``, but use their own resolver to avoid conflicts.
12681260
12691261``has_and_belongs_to_many`` associations do not support polymorphism.
12701262
0 commit comments