123456789_123456789_123456789_123456789_123456789_

Exception: Mongoid::Errors::InvalidRelation

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/invalid_relation.rb

Overview

This error is raised when trying to create an association that conflicts with an already defined method.

Constant Summary

MongoidError - Inherited

BASE_KEY

Class Method Summary

Instance Attribute Summary

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(klass, name) ⇒ InvalidRelation

Create the new error.

Examples:

Create the error.

InvalidRelation.new(person, :crazy_relation_name)

Parameters:

  • klass (Class)

    The document class.

  • name (Symbol)

    The method name.

[ GitHub ]

  
# File 'lib/mongoid/errors/invalid_relation.rb', line 18

def initialize(klass, name)
  super(
      compose_message(
          "invalid_relation",
          {
              name: name,
              origin: origin(klass, name),
              file: location(klass, name)[0],
              line: location(klass, name)[1]
          }
      )
  )
end

Instance Method Details

#location(klass, name) ⇒ Array<String, Integer> (private)

Get the location of the association definition.

Examples:

Get the location of the method on the filesystem.

error.location(Person, :crazy_method_name)

Parameters:

  • klass (Class)

    The document class.

  • name (Symbol)

    The method name.

Returns:

[ GitHub ]

  
# File 'lib/mongoid/errors/invalid_relation.rb', line 56

def location(klass, name)
  @location ||=
      (klass.instance_method(name).source_location || [ "Unknown", 0 ])
end

#origin(klass, name) ⇒ Class | Module (private)

Get the queryable of the method.

Examples:

Get the originating class or module.

error.queryable(Person, :crazy_method_name)

Parameters:

  • klass (Class)

    The document class.

  • name (Symbol)

    The method name.

Returns:

  • (Class | Module)

    The originating class or module.

[ GitHub ]

  
# File 'lib/mongoid/errors/invalid_relation.rb', line 43

def origin(klass, name)
  klass.instance_method(name).owner
end