将rails查询结果转换为树

时间:2015-11-13 23:41:43

标签: ruby-on-rails postgresql

我对ruby / rails很新。我有一个AR查询,在postgres-突变和效果中连接2个表。突变有很多影响。 该查询的结果类似于:

mutation_id  |  chr  | pos |  gene  |  eff_impact | eff_transcript
---------------------------------------------------------------------
1            |  1    | 123 | JAK   |  bad         | tx123
1            |  1    | 123 | JAK   |  worse       | tx456
2            |  4    | 456 | KRAS  |  fine        | tx234
2            |  4    | 456 | KRAS  |  scary       | tx483
3            |  7    | 789 | SMURF |  helpful     | tx987

这给了我一个平坦的结果集,其中包含来自突变表的重复值,因为突变有很多影响。

我最终需要的是一个具有树结构的哈希数组,如下所示:

[
{"mutation_id" => 1, 
 "chr" => 1, 
 "pos" => 123, 
 "gene" => "JAK", 
 "effects" => [
  {"eff_impact" => "bad", "eff_transcript" => "tx123"},
  {"eff_impact" => "worse", "eff_transcript" => "tx456"},
 ]
},
{"mutation_id" => 2, 
 "chr" => 4, 
 "pos" => 546, 
 "gene" => "KRAS", 
 "effects" => [
  {"eff_impact" => "fine", "eff_transcript" => "tx234"},
  {"eff_impact" => "scary", "eff_transcript" => "tx483"},
 ]
},
{"mutation_id" => 3, 
 "chr" => 7, 
 "pos" => 546, 
 "gene" => "SMURF", 
 "effects" => [
  {"eff_impact" => "helpful", "eff_transcript" => "tx987"}
 ]
}
] 

结果是~100k行。如果可能的话,我更希望初始查询返回具有此树结构的AR结果。

由于

0 个答案:

没有答案