user742736
user742736

Reputation: 2729

Ruby on Rails - Redmine array grouping/ordering

I'm still new to Ruby on Rails so I am still trying to get my head around how it all works.

I'm using redmine and I am trying to grab a list of projects and order them by the parent_id only problem with this is the parent project is group with the other parent projects

which looks like this:

parent_1
parent_2
parent_3
child_1a
child_1b
child_3a
child_3b

What I am trying to achieve is:

parent_1
child_1a
child_1b
parent_2
parent_3
child_3a
child_3b

Code Below html = '' html += '' + "Issues" + ''

  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

Upvotes: 0

Views: 152

Answers (1)

mechanicalfish
mechanicalfish

Reputation: 12836

Redmine uses a patched version of awesome_nested_set for the project tree. You should be ordering by lft if you want the projects to be ordered hierarchically. So:

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

Upvotes: 1

Related Questions