2012-01-23 6 views
1

레일 3.1에서 네임 스페이스에 문제가 있습니다. 나는 수업을 가지고있다.모델 네임 스페이스 레일 문제

#/app/models/a.rb 
class a 
    #some methods 
    def self.method_from_a 
    #does things 
    end 
end 

하지만 다른 이름 공간에 같은 이름을 가진 다른 클래스가 있습니다.

#/app/models/b/a.rb 
class b::a 
    def method 
    return a.method_from_a 
    end 
end 

나는 나 호출 할 때 :: a.method를 내가 얻을 수 있지만 :

NameError: uninitialized constant b::a::a 

나는 그것이 간단한 솔루션입니다 확신, 난 그냥 누락하고있다. ::

답변

3

프리픽스 a :

class b::a 
    def method 
    return ::a.method_from_a 
    end 
end 

이 (즉, 범위 연산자)도 here 설명 :

상수 클래스 또는 모듈 내에 정의를 어디서나 내의 꾸밈 액세스 될 수있다 클래스 또는 모듈. 클래스 또는 모듈 외부에서 은 범위 연산자 인 ::'' prefixed by an expression that returns the appropriate class or module object. Constants defined outside any class or module may be accessed unadorned or by using the scope operator :: ''을 사용하여 액세스 할 수 있습니다.

그런데 Ruby 클래스 이름은 대문자로 시작해야합니다.

+0

고마워요. 나는 내가 방금 무언가를 놓치고 있다는 것을 알았다. – Red