1
我一直在玩這個,想得到第二個意見。ruby/rails - 從活動記錄中創建一棵倒轉的樹找
這是問題所在 - 我要訂購這些結果通過
誰的人報告(即那裏的老闆)
---
- !ruby/object:History
attributes:
id: 1392
job_title: Global Leader
reports_to:
attributes_cache: {}
- !ruby/object:History
attributes:
id: 1393
job_title: Programme Organiser
reports_to: 1392
attributes_cache: {}
- !ruby/object:History
attributes:
id: 1394
job_title: Programme Lead
reports_to: 1393
attributes_cache: {}
他們將在JSON輸出,以便看起來像這樣
{id: 1394, some_attributes, children: {id: 1393, some_attributes, children: { etc }}
我寫了這個功能,但它開始看起來非常糟糕
candidates.each do |candidate, index|
if !candidate.reports_to.present?
structure << candidate
candidates.to_a.delete_if {|candidate_inst| candidate_inst == candidate}
candidates.each do |child|
if child.reports_to == candidate.candidate_id
# add child to children of parent
end
end
end
必須有一個更清潔的方式做到這一點?
謝謝,亞歷克斯