0

has_many ストアの Account クラスがあります。Store クラスには、そのアカウントの他のすべてのストアを返すルーチンがあります。

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

4

2 に答える 2

0

ほとんどの場合、このように見えます。[@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]

最も簡単な修正は、既にレンダリングされた ID を渡すことです。

def as_json(rendered_ids = [])
于 2011-12-01T19:25:26.873 に答える
0

私はそれを理解したと思います。これは機能しているようです:

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

クリス

于 2011-12-02T16:45:12.993 に答える