2011-12-01 3 views
0

has_many 상점을 가진 Account 클래스가 있습니다.레일스 as_json 루틴에서 무한 재귀 호출하기

내가 포함
def other_stores 
    if account then 
    account.stores.find(:all,:conditions=>"id != "+id.to_s) 
    else 
    [] 
    end 
end 

: 내 as_json 루틴 other_stores 다음을 참조, 나는 CPU를 고정하고 중지 스토어 클래스에서 해당 계정에 대한 모든 다른 매장을 반환하는 루틴이있다. other_stores에서 무한 재귀라고 가정합니다. 어떤 아이디어? 재귀를 막을 방법이 있습니까?

루비 1.9.2-P136은, 그것은 가장 가능성이 보이는 3.0.3

답변

0

레일,의 당신이 가장 쉬운 수정 이미 가지고있는 IDS에 전달하는 것입니다 [@store1, @store2]

@store1.as_json 
#as_json calls @store1.other_stores() ==> [@store2] 
    @store2.as_json 
    #as_json calls @store2.other_stores() ==> [@store1] 
    @store1.as_json 
    #calls @store1.other_stores() other_stores ==> [@store2] 

있다고 가정 해 봅시다 다음과 같이 렌더링되었습니다.

def as_json(rendered_ids = []) 
+0

감사합니다. 나는 그 접근법에 대해 궁금해했다. 나는 그것을 알아 냈다고 생각한다. 아래를 보라. – user1075975

0

생각해 보았습니다. 이것은 작동하는 것 같군 :

def as_json(options={}) 
    super(:methods => [:blah, :etc, 
        :other_stores => {:except => :other_stores} ]) 
end 

크리스에게 응답을

+0

아니, 틀렸어 (바보). 그것은 other_stores가 json에 전혀 나타나지 않게합니다. – user1075975

관련 문제