2013-09-08 2 views
3

데이터베이스의 두 항목 사이에 belongs_to 연결을 구현하는 라이브러리를 사용하고 있습니다. 이것은 내가 필요한 행동이 아니기 때문에 prepend을 통해이 메소드를 오버라이드하고 싶습니다. 그러나 pry는 원래 방법이 여전히 호출됨을 알려줍니다. 나는 이중 체크를했고 나는 루비 2.0을 사용하고있다. 앞에 추가됩니다재정의 된 메서드가 여전히 호출됩니다.

코드 :

참고 : 나는 또한이 클래스의 메소드를 오버라이드 (override)하는 시도했지만 여전히 발생하지 않습니다

module Associations 
    module ClassMethods 

    [...] 
    #Add the attributeName to the belongsToAttributes 
    #and add a field in the list for the IDs 
    def belongs_to(attr_name) 
     @belongsToAttributes ||= [] 
     @belongstoAttributes << attr_name 

     create_attr attr_name.to_s 
     attribute belongs_to_string.concat(attr_name.to_s).to_sym 
    end 

    def belongsToAttributes 
     @belongsToAttributes 
    end  
    end 

    def self.included(base) 
    base.extend(ClassMethods) 
    end 
end 

# prepend the extension 
Couchbase::Model.send(:prepend, Associations) 

나는이 수업 시간에 이것을 사용 내가 놀리려는 확인하면

require 'couchbase/model' 

class AdServeModel < Couchbase::Model 

[...] 
    #I tried to add the belongs_to method like this 
    #def belongs_to(attr_name) 
    # @belongsToAttributes ||= [] 
    # @belongstoAttributes << attr_name 

    # create_attr attr_name.to_s 
    # attribute belongs_to_string.concat(attr_name.to_s).to_sym 
    # end 

    # def belongsToAttributes 
    # @belongsToAttributes 
    # end 
end 

그것은 내가 this 메서드 호출에 결국 내게 보여줍니다

def self.belongs_to(name, options = {}) 
    ref = "#{name}_id" 
    attribute(ref) 
    assoc = name.to_s.camelize.constantize 
    define_method(name) do 
    assoc.find(self.send(ref)) 
    end 
end 

내가 잘못하고있는 것에 대한 어떠한 조언도 감사 할 것입니다.

편집 : 아리 쇼의 게시물에 포함

self.prepended(base) 
    class << base 
     prepend ClassMethods 
    end 
    end 

end 
# prepend the extension 
Couchbase::Model.send(:prepend, Associations) 

때문에 중요한 포인터 나는 그의 대답을 받아이 문제를 해결하기 :이 같은 문제를 해결 좋아. 그는 내가 부르기 원하는 방법을 연장하고 앞당기는 것에 대한 요점을 놓쳤다. 메소드를 추가 할 때 발생하는 문제에 대한 자세한 내용은 this 질문을 참조하십시오.

답변

1

게시 한 pry 추적에 따르면 원숭이 패치를 원했던 방법은 인스턴스 메소드가 아닌 AdServeModel의 클래스 메소드입니다.

  • 당신의 Associations 모듈 접근 방식, 당신이 Module#prepend 기존 클래스에 모듈 prepend에, 그러나, 당신이 모듈이 포함되어있는 경우에만 호출 될 self.included 후크 방법을 썼다 호출되는 문제는 (앞에 추가하지). 대신에 Module#prepended 훅을 써야합니다.

  • 직접 재정의 방식의 문제점은 클래스 메서드가 아니라 인스턴스 메서드를 실제로 재정의했기 때문입니다.

    내가 볼 예
require 'couchbase/model' 

class AdServeModel < Couchbase::Model 
    class << self 
    # save the original method for future use, if necessary 
    alias_method :orig_belongs_to, :belongs_to 

    def belongs_to(attr_name) 
     @belongsToAttributes ||= [] 
     @belongstoAttributes << attr_name 

     create_attr attr_name.to_s 
     attribute belongs_to_string.concat(attr_name.to_s).to_sym 
    end 

    def belongsToAttributes 
     @belongsToAttributes 
    end 
    end 
end 
+0

아,이 호출'alias_method_chaining' 내 생각 :이 같은해야 하는가? 나는 실제로 그것을 피하려고 노력한다. Module # prepend에 대한 훅을 이미 변경했지만 아무 쓸모가 없습니다. http://stackoverflow.com/questions/18683750/how-to-prepend-classmethods 질문을 참조하여보다 기본적인 문제에 대한 답을 찾으십시오. – Nessuno

관련 문제