2013-11-28 90 views
0

我還是Ruby on Rails的新手,所以我仍然試圖讓我的腦海中圍繞它是如何工作的。Ruby on Rails - Redmine數組分組/訂購

我使用的管理平臺,我試圖抓住的項目清單,並由PARENT_ID只有這個問題責令其是父項目是與其他父母項目

它看起來像這組:

parent_1 
parent_2 
parent_3 
child_1a 
child_1b 
child_3a 
child_3b 

我想實現的是:

parent_1 
child_1a 
child_1b 
parent_2 
parent_3 
child_3a 
child_3b 

下面的代碼 HTML = '' HTML + = '' + 「問題」 + ''

all_projects = Project.visible().order("parent_id ASC").where("status != 5") 

    my_projects = [] 

    #lets build our projects and issues array 

    all_projects.each do |project| 
    issues = Issue.visible().joins(:status).where("issue_statuses.is_closed != 1 AND status_id !=3 AND project_id = :project_id", {project_id: project.id}).order("issues.due_date DESC, priority_id DESC") 
    my_projects << Array.new([project, Array.new(issues)]) 
    end 

    if my_projects.first 
    html += '<ul class="projects_list">' 
    my_projects.each do |project, issues| 



      html += "<li class='project_name'>" 
      html += link_to_project(project) 
      html += "<ul>" 
      issues.each do |issue| 

      html += link_to_issue(issue) 
      end 
      html += "</ul>" 

      html += "</li>" 

    end 
    html += '</ul>' 
    else 
    html += '<p class="nodata">' + l(:label_no_data) + '</p>' 
    end 



    html += '</div>' 



    return html 
end 

回答

1

管理平臺使用的awesome_nested_set爲項目樹一個補丁版本。如果您希望項目按層次順序排列,您應該按lft進行訂購。所以:

all_projects = Project.visible.order("lft ASC").where("status != 5") 
+0

相反acts_as_nested_set的,管理平臺使用的[awesome_nested_set(https://github.com/collectiveidea/awesome_nested_set)一個補丁版本。因爲這種工作類似,你的答案仍然完全有效。 –

+0

感謝您的澄清,我認爲awesome_nested_set是我們修改的一部分:) – mechanicalfish