필자는 ActiveRecord 쿼리에서 반환 된 레코드를 근본적으로 수정하는 Rails 3 gem을 빌드하고있다. 내가하고있는 일 중 하나는 method_missing
과 respond_to?
메서드를 재정의하지만 내 respond_to?
정의에서 "SystemStackError : stack level too deep"오류가 발생하는 무한 루프가 발생하는 것으로 보입니다. 여기 Object.respond_to? 무한 루프에 갇혀있다
def respond_to?(name, *args)
super(name, *args) || parent_association.respond_to?(name)
end
def method_missing(name, *args, &block)
if parent_association.respond_to?(name)
parent_association.send(name, *args, &block)
else
super(name, *args, &block)
end
end
def parent_association
send(parent_association_name) # Essentially yields another ActiveRecord
# instance (e.g.: instance of User), but
# never returns itself.
end
, 내가보고 출력 "후"약간의 "이전"과 함께 respond_to?
구조 조정 .
before (_run__374051839217347232__initialize__1707831318230746190__callbacks)
after
before (_run__374051839217347232__validation__1707831318230746190__callbacks)
after
before (_run__374051839217347232__validate__1707831318230746190__callbacks)
after
before (_run__374051839217347232__save__1707831318230746190__callbacks)
after
before (_run__374051839217347232__create__1707831318230746190__callbacks)
after
before (created_at)
after
before (created_on)
after
...
그러나 언제든지 나는 것을하는 찾기 콜백을 참조하십시오
def respond_to?(name, *args)
return true if super(name, *args)
puts "before (#{name})"
result = parent_association.respond_to?(name)
puts "after"
result
end
실행
, 예상대로 다양한 콜백과 각에 대한 전과 호출 후 하나와 실행 방법을 속성 것으로 보인다 무한 루프에 걸린 것으로 나타납니다 :before (_run__374051839217347232__find__1707831318230746190__callbacks)
before (_run__374051839217347232__find__1707831318230746190__callbacks)
before (_run__374051839217347232__find__1707831318230746190__callbacks)
before (_run__374051839217347232__find__1707831318230746190__callbacks)
before (_run__374051839217347232__find__1707831318230746190__callbacks)
before (_run__374051839217347232__find__1707831318230746190__callbacks)
before (_run__374051839217347232__find__1707831318230746190__callbacks)
before (_run__374051839217347232__find__1707831318230746190__callbacks)
...
SystemStackError: stack level too deep
내 respond_to?
, 그때 모든 것이 원활하게 실행하기 위해 나타납니다 해킹하는 경우 :
def respond_to?(name, *args)
return true if super(name, *args)
return false if name =~ /^_run_.*_find_.*_callbacks$/
parent_association.respond_to?(name)
end
무엇이 내가이 해킹이 필요한 것일까 요? 어떻게 피할 수 있습니까? parent_association
새로운 AR 객체를 반환하는 경우
정확한 언어를 사용하면 더 명확 해집니다. 'parent_association_name'은 변수가 아니며'employee'가 항상 정의되어 있다고 생각합니다.로드되지 않았을 수도 있습니다. 실제로 "콜백"을 의미하는지 확신 할 수 없습니다. 나는 당신이 묘사하고있는 것이 내가 말하는 것과 당신이 그것을 어떻게 고쳐 놓았는지에 대해 아직도 확실하지 않습니다. –