Exception: Mongoid::Errors::UnrecognizedModelAlias
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
MongoidError ,
StandardError
|
|
Instance Chain:
self,
MongoidError ,
StandardError
|
|
Inherits: |
Mongoid::Errors::MongoidError
|
Defined in: | lib/mongoid/errors/unrecognized_model_alias.rb |
Overview
Raised when a polymorphic association is queried, but the type of the association cannot be resolved. This usually happens when the data in the database references a type that no longer exists.
For example, consider the following model:
class Manager
include Mongoid::Document
belongs_to :unit, polymorphic: true
end
Imagine there is a document in the managers
collection that looks something like this:
{ _id: ..., unit_id: ..., unit_type: 'Department::Engineering' }
If, at some point in your refactoring, you rename the Department::Engineering
model to something else, ::Mongoid
will no longer be able to resolve the type of this association, and asking for manager.unit
will raise this exception.
To fix this exception, you can add an alias to the model class so that it can still be found, even after renaming it:
module Engineering
class Department
include Mongoid::Document
identify_as 'Department::Engineering'
# ...
end
end
Better practice would be to use unique strings instead of class names to identify these polymorphic types in the database (e.g. ‘dept’ instead of ‘Department::Engineering’).
Constant Summary
MongoidError
- Inherited
Class Method Summary
- .new(model_alias) ⇒ UnrecognizedModelAlias constructor
Instance Attribute Summary
MongoidError
- Inherited
Instance Method Summary
MongoidError
- Inherited
#compose_message | Compose the message. |
#translate | Given the key of the specific error and the options hash, translate the message. |
#translate_problem | Create the problem. |
#translate_resolution | Create the resolution. |
#translate_summary | Create the summary. |
Constructor Details
.new(model_alias) ⇒ UnrecognizedModelAlias
# File 'lib/mongoid/errors/unrecognized_model_alias.rb', line 43
def initialize(model_alias) super( ( 'unrecognized_model_alias', model_alias: model_alias.inspect ) ) end