class Mongoid::Errors::InvalidRelation

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

Public Class Methods

new(klass, name) click to toggle source

Create the new error.

@example Create the error.

InvalidRelation.new(person, :crazy_relation_name)

@param [ Class ] klass The document class. @param [ Symbol ] name The method name.

Calls superclass method
# 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

Private Instance Methods

location(klass, name) click to toggle source

Get the location of the association definition.

@example Get the location of the method on the filesystem.

error.location(Person, :crazy_method_name)

@param [ Class ] klass The document class. @param [ Symbol ] name The method name.

@return [ Array<String, Integer> ] The location of the method.

# 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) click to toggle source

Get the queryable of the method.

@example Get the originating class or module.

error.queryable(Person, :crazy_method_name)

@param [ Class ] klass The document class. @param [ Symbol ] name The method name.

@return [ Class | Module ] The originating class or module.

# File lib/mongoid/errors/invalid_relation.rb, line 43
def origin(klass, name)
  klass.instance_method(name).owner
end