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