Ruby:返回按多列分组的数据

时间:2012-11-18 07:03:50

标签: sql ruby-on-rails ruby hashmap hashtable

我正在为网络服务尝试群组数据。

Web服务在Ruby on Rails上运行,我在我的API控制器中工作(让我们称之为我的projects_controller的索引操作。

表模式如下所示(由于NDA原因,数据类型和示例已更改)。不幸的是,这里的例子表明我将员工和项目分成不同的表,但请暂时忽略它。这是我给出的数据:

COLUMNS:
employee, e_id, company, hire_date, project_name, project_due_date

ROWS:
John, 12345, XYZ, 01-01-2001, Project_A, 12-31-2012
John, 12345, XYZ, 01-01-2001, Project_B, 03-15-2013
John, 12345, XYZ, 01-01-2001, Project_C, 06-25-2013
Jane, 98765, XYZ, 05-22-2003, Project_Q, 01-15-2013
Jane, 98765, XYZ, 05-22-2003, Project_W, 02-25-2013
Jane, 98765, XYZ, 05-22-2003, Project_E, 08-01-2013

为了减少数据传输,我想按如下方式返回上述内容:

[
    {
        "employee":"John",
        "e_id":"12345",
        "company":"XYZ",
        "hire_date":"01-01-2001",
        "projects":[
                     { "project_name":"Project_A", "project_due_date":"12-31-2012" },
                     { "project_name":"Project_B", "project_due_date":"03-15-2013" },
                     { "project_name":"Project_C", "project_due_date":"06-25-2013" }
                   ]
    },
    {
        "employee":"Jane",
        "e_id":"98765",
        "company":"XYZ",
        "hire_date":"05-22-2003",
        "projects":[
                     { "project_name":"Project_Q", "project_due_date":"01-15-2013" },
                     { "project_name":"Project_W", "project_due_date":"02-25-2013" },
                     { "project_name":"Project_E", "project_due_date":"08-01-2013" }
                   ]
    }
]

我似乎无法找出将SQL查询结果(行)分组到理想数据中的有组织哈希值的最佳方法。我想我需要一些.each和哈希来后处理我的SQL调用返回的数据,但我似乎无法弄清楚“Ruby”的方式(我也不是一个经验丰富的Ruby开发人员,所以任何参考链接也会受到赞赏,所以我可以阅读解决方案。)

我该如何做到这一点?

[编辑]

我正在对Project对象执行SQL查询。我的控制器如下:

def index
   sql = "SELECT employee, e_id, company, hire_date, project_name, project_due_date
     FROM projects
     AND created_at = (SELECT created_at FROM projects ORDER BY created_at DESC LIMIT 1)
     ORDER BY company, employee, project_due_date"

   result = Project.find_by_sql(sql)
   respond_with(result)
end

我得到的数据是一系列Project对象,格式如下

RUBY DEBUGGER:

(rdb:2) result
[#<Project employee: "John", e_id: 12345, company: "XYZ", hire_date: "01-01-2001", project_name: "Project_A", project_due_date: "12-31-2012">,
 #<Project employee: "John", e_id: 12345, company: "XYZ", hire_date: "01-01-2001", project_name: "Project_B", project_due_date: "03-15-2013">,
 #<Project employee: "John", e_id: 12345, company: "XYZ", hire_date: "01-01-2001", project_name: "Project_C", project_due_date: "06-25-2013">,
 #<Project employee: "Jane", e_id: 98765, company: "XYZ", hire_date: "05-22-2003", project_name: "Project_Q", project_due_date: "01-15-2013">,
 #<Project employee: "Jane", e_id: 98765, company: "XYZ", hire_date: "05-22-2003", project_name: "Project_W", project_due_date: "02-25-2013">,
 #<Project employee: "Jane", e_id: 98765, company: "XYZ", hire_date: "05-22-2003", project_name: "Project_E", project_due_date: "08-01-2013">]

[编辑2]

我知道我可以用非常幼稚的非Ruby方式解决这个问题,但我想知道让它运行的正确方法。基本解决方案可以包括迭代结果数组并逐行解析数据,将员工数据保存为临时哈希,将项目数据保存为哈希数组。当迭代发生给新员工时,将先前员工数据的数据保存在数组中,并为下一个员工重置临时数组/哈希值。非常难看,但很有可能。

但是,必须有Ruby方式。请帮忙!

1 个答案:

答案 0 :(得分:1)

以请求的形式生成分组数据:

grouped_data = data.group_by do |project|
  [project.employee, project.e_id, project.company, project.hire_date]
end.map do |k, v|
  { 
    "employee"  => k[0],
    "e_id"      => k[1],
    "company"   => k[2],
    "hire_date" => k[3],

    "projects" => v.map do |p|
      {
        "project_name"     => p.project_name,
        "project_due_date" => p.project_due_date
      }
    end
  }
end

最后使用to_json生成JSON格式的版本,例如:

grouped_data.to_json