2010-06-26 4 views
0

lib/폴더 내에 CommonFunctions Module이 있습니다. CommonFunctions 모듈을 포함하는 질문 모델이 있습니다. 이제 Question.favorite와 같은 CommonFunctions의 가장 좋아하는 기능에 액세스하려고합니다. 하지만 NoMethodError가 나타납니다. 나는 코드를 포함시켰다. 아무도 내가 실수루비 모듈 포함 질문

오류

lib 디렉토리 내부
NoMethodError: undefined method `favorite' for Class:0x00000100e11508 

/CommonFunctions.rb 응용 프로그램/모델 내부

module CommonFunctions 
    def favorite(object_id) 
    end 
end 

/Question.rb을하고있는 중이 야 어디 있는지 말해 주시겠습니까

require 'lib/CommonFunctions.rb' 
class Question 
    extend CommonFunctions 
end 

내가 스크립트/콘솔에서

Question.favorite(1) 

감사


다음 코드를 실행하고이 How do I properly include a module and call module functions from my Rails model?

답변

2

귀하의 코드 중복이 올바른지이었다. 콘솔에로드 된 클래스의 최신 버전을 가지고 있는지 확인하십시오 (reload!을 시도하십시오).

부연 설명 : CommonFunctions.rb의 이름을 common_functions.rb로 바꾸면 레일즈가 자동으로로드되므로 필요하지 않습니다.

+0

매우 감사 등의 방법을 정의하여 대신

module CommonFunctions def self.favorite(object_id) end end 

아래 코드를 사용합니다 :) – felix

1

모듈 메소드는 클래스 메소드가되기를 원할 때 인스턴스 메소드입니다. 단어 "자기"클래스 메소드 (또는 정적)