Constant resolution operator `::`
Also, known as scope resolution operator
.
When we work with namespace it's very common to override some objects, etc that we already have. This could cause us headaches when we try to access a specific object and that object has the same name as our namespace. To have a clearer idea, let's review the next example, imagine that we have the next nested modules:
module FHIR
module Services
module Appointment
def appointment
@appointment ||= Appointment.find(id)
end
end
end
end
If you do Appointment.find(id)
you will get an error similar to this:
NoMethodError: undefined method 'find' for FHIR::Services::Appointment:Module
.
That ^ is because the Appointment
is doing reference to the namespace FHIR::Services::Appointment
(local scope) instead of the Appointment
model (global scope).
The constant resolution operator
will help us to resolve this. If you put:
::Appointment.find(id)
This will work because is referencing the global namespace which is the Appointment model this way you can access directly to the model instead of the current namespace.