我有如下表格记录:
我根据am
,pm
和none
对它们进行分组。
我真正需要将none
到7
7
列的group_by分解为none
的总记录为14
。
查询我正在使用:
SlotFormula.select([:id,:doorman,:day]).where(:zone_id => 1).order('created_at
asc').group_by { |result| [result.doorman] }
查询的当前结果是:
=> {["am"]=>
[
#<SlotFormula id: 1659, day: "Monday", doorman: "am">,
#<SlotFormula id: 1660, day: "Tuesday", doorman: "am">,
#<SlotFormula id: 1661, day: "Wednesday", doorman: "am">,
#<SlotFormula id: 1662, day: "Thursday", doorman: "am">,
#<SlotFormula id: 1663, day: "Friday", doorman: "am">,
#<SlotFormula id: 1664, day: "Saturday", doorman: "am">,
#<SlotFormula id: 1665, day: "Sunday", doorman: "am">],
["pm"]=>
[
#<SlotFormula id: 1666, day: "Monday", doorman: "pm">,
#<SlotFormula id: 1667, day: "Tuesday", doorman: "pm">,
#<SlotFormula id: 1668, day: "Wednesday", doorman: "pm">,
#<SlotFormula id: 1669, day: "Thursday", doorman: "pm">,
#<SlotFormula id: 1670, day: "Friday", doorman: "pm">,
#<SlotFormula id: 1671, day: "Saturday", doorman: "pm">,
#<SlotFormula id: 1672, day: "Sunday", doorman: "pm">],
["none"]=>[
#<SlotFormula id: 1673, day: "Monday", doorman: "none">,
#<SlotFormula id: 1674, day: "Tuesday", doorman: "none">,
#<SlotFormula id: 1675, day: "Wednesday", doorman: "none">,
#<SlotFormula id: 1676, day: "Thursday", doorman: "none">,
#<SlotFormula id: 1677, day: "Friday", doorman: "none">,
#<SlotFormula id: 1678, day: "Saturday", doorman: "none">,
#<SlotFormula id: 1679, day: "Sunday", doorman: "none">,
#<SlotFormula id: 1680, day: "Monday", doorman: "none">,
#<SlotFormula id: 1681, day: "Tuesday", doorman: "none">,
#<SlotFormula id: 1682, day: "Wednesday", doorman: "none">,
#<SlotFormula id: 1683, day: "Thursday", doorman: "none">,
#<SlotFormula id: 1684, day: "Friday", doorman: "none">,
#<SlotFormula id: 1685, day: "Saturday", doorman: "none">,
#<SlotFormula id: 1686, day: "Sunday", doorman: "none">
]}
需要的预期结果:
=> {["am"]=>
[
#<SlotFormula id: 1659, day: "Monday", doorman: "am">,
#<SlotFormula id: 1660, day: "Tuesday", doorman: "am">,
#<SlotFormula id: 1661, day: "Wednesday", doorman: "am">,
#<SlotFormula id: 1662, day: "Thursday", doorman: "am">,
#<SlotFormula id: 1663, day: "Friday", doorman: "am">,
#<SlotFormula id: 1664, day: "Saturday", doorman: "am">,
#<SlotFormula id: 1665, day: "Sunday", doorman: "am">],
],
["pm"]=>
[
#<SlotFormula id: 1666, day: "Monday", doorman: "pm">,
#<SlotFormula id: 1667, day: "Tuesday", doorman: "pm">,
#<SlotFormula id: 1668, day: "Wednesday", doorman: "pm">,
#<SlotFormula id: 1669, day: "Thursday", doorman: "pm">,
#<SlotFormula id: 1670, day: "Friday", doorman: "pm">,
#<SlotFormula id: 1671, day: "Saturday", doorman: "pm">,
#<SlotFormula id: 1672, day: "Sunday", doorman: "pm">],
],
["none"]=>[
#<SlotFormula id: 1673, day: "Monday", doorman: "none">,
#<SlotFormula id: 1674, day: "Tuesday", doorman: "none">,
#<SlotFormula id: 1675, day: "Wednesday", doorman: "none">,
#<SlotFormula id: 1676, day: "Thursday", doorman: "none">,
#<SlotFormula id: 1677, day: "Friday", doorman: "none">,
#<SlotFormula id: 1678, day: "Saturday", doorman: "none">,
#<SlotFormula id: 1679, day: "Sunday", doorman: "none">,
],
["none"]=>[
#<SlotFormula id: 1680, day: "Monday", doorman: "none">,
#<SlotFormula id: 1681, day: "Tuesday", doorman: "none">,
#<SlotFormula id: 1682, day: "Wednesday", doorman: "none">,
#<SlotFormula id: 1683, day: "Thursday", doorman: "none">,
#<SlotFormula id: 1684, day: "Friday", doorman: "none">,
#<SlotFormula id: 1685, day: "Saturday", doorman: "none">,
#<SlotFormula id: 1686, day: "Sunday", doorman: "none">
]
}
修改
我的另一个问题是:如何将它们显示为列而非行?
所以,
r
表示下表中的result
n
表示下表中的none
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
am am(mo r) am(tu r) am(we r) am(th r) am(fr r) am(sa r) am(su r)
pm pm(mo r) pm(tu r) pm(we r) pm(th r) pm(fr r) pm(sa r) pm(su r)
n n(mo r) n(tu r) n(we r) n(th r) n(fr r) n(sa r) n(su r)
n n(mo r) n(tu r) n(we r) n(th r) n(fr r) n(sa r) n(su r)
n n(mo r) n(tu r) n(we r) n(th r) n(fr r) n(sa r) n(su r)
am am(mo r) am(tu r) am(we r) am(th r) am(fr r) am(sa r) am(su r)
pm pm(mo r) pm(tu r) pm(we r) pm(th r) pm(fr r) pm(sa r) pm(su r)
n n(mo r) n(tu r) n(we r) n(th r) n(fr r) n(sa r) n(su r)
n n(mo r) n(tu r) n(we r) n(th r) n(fr r) n(sa r) n(su r)
n n(mo r) n(tu r) n(we r) n(th r) n(fr r) n(sa r) n(su r)
如果有人有更好的方法来处理它。请分享。
答案 0 :(得分:1)
我真正需要将group_by分解为
7
7
列 因为none
的总记录为14
。
您可以使用现有的同一查询,只需再添加一次,使用Array#map
和Enumerable#each_slice
来打破每个群组(["am"]
,["pm"]
,["none"]
)分为七个对象组:
SlotFormula.select(:id, :doorman, :day)
.where(zone_id: 1)
.order(created_at: :asc)
.group_by(&:doorman)
.map { |k, v| { k => v.each_slice(7).to_a } }
首先,您会发现group_by
已更改为group_by(&:doorman)
,这将获得与使用group_by { |elem| elem.doorman }
完全相同的结果(请注意[
和{{1}移除的地方,而不是]
,您将获得["pm"]
。
其次,我们添加了"pm"
,将.map
应用于每个组的每个元素,这些元素在多个七个组中分割任何大于7的组。
输出:
each_slice(7)
这会稍微改变您的原始格式,例如[
{
"am"=> [
[
#<SlotFormula:0x007f8128968ec0 id: 1, day: "Monday", doorman: "am">,
#<SlotFormula:0x007f8128968d80 id: 2, day: "Tuesday", doorman: "am">,
#<SlotFormula:0x007f8128968c40 id: 3, day: "Wednesday", doorman: "am">,
#<SlotFormula:0x007f8128968b00 id: 4, day: "Thursday", doorman: "am">,
#<SlotFormula:0x007f81289689c0 id: 5, day: "Friday", doorman: "am">,
#<SlotFormula:0x007f8128968880 id: 6, day: "Saturday", doorman: "am">,
#<SlotFormula:0x007f8128968740 id: 7, day: "Sunday", doorman: "am">
]
]
},
{
"pm"=> [
[
#<SlotFormula:0x007f8128968600 id: 8, day: "Monday", doorman: "pm">,
#<SlotFormula:0x007f81289684c0 id: 9, day: "Tuesday", doorman: "pm">,
#<SlotFormula:0x007f8128968380 id: 10, day: "Wednesday", doorman: "pm">,
#<SlotFormula:0x007f8128968240 id: 11, day: "Thursday", doorman: "pm">,
#<SlotFormula:0x007f8128968100 id: 12, day: "Friday", doorman: "pm">,
#<SlotFormula:0x007f8128973f78 id: 13, day: "Saturday", doorman: "pm">,
#<SlotFormula:0x007f8128973e38 id: 14, day: "Sunday", doorman: "pm">
]
]
},
{
"none"=> [
[
#<SlotFormula:0x007f8128973cf8 id: 15, day: "Monday", doorman: "none">,
#<SlotFormula:0x007f8128973bb8 id: 16, day: "Tuesday", doorman: "none">,
#<SlotFormula:0x007f8128973a78 id: 17, day: "Wednesday", doorman: "none">,
#<SlotFormula:0x007f8128973938 id: 18, day: "Thursday", doorman: "none">,
#<SlotFormula:0x007f81289737f8 id: 19, day: "Friday", doorman: "none">,
#<SlotFormula:0x007f81289736b8 id: 20, day: "Saturday", doorman: "none">,
#<SlotFormula:0x007f8128973578 id: 21, day: "Sunday", doorman: "none">
], [
#<SlotFormula:0x007f8128973438 id: 22, day: "Monday", doorman: "none">,
#<SlotFormula:0x007f81289732f8 id: 23, day: "Tuesday", doorman: "none">,
#<SlotFormula:0x007f81289731b8 id: 24, day: "Wednesday", doorman: "none">,
#<SlotFormula:0x007f8128973078 id: 25, day: "Thursday", doorman: "none">,
#<SlotFormula:0x007f8128972f38 id: 26, day: "Friday", doorman: "none">,
#<SlotFormula:0x007f8128972df8 id: 27, day: "Saturday", doorman: "none">,
#<SlotFormula:0x007f8128972cb8 id: 28, day: "Sunday", doorman: "none">
]
]
}
]
个Array
个对象(Hash
个Hash
对象的原始格式,但主要变化是添加了一个更多级别的数组,其中所有内容都按7个对象的块进行分组。
Array
和am
组不太清楚,因为两者都有7个项目,所以你看到一个数组有一个数组(里面有7个元素)。但是使用pm
,您可以看到一个内部有两个数组的数组,每个数组包含7个项目(即对象)。
我推荐这种方法,因为使用none
等密钥迭代和获取数据会更容易,例如:
:pm
但是,如果你需要你已有的格式,也可以获得,但我们需要Enumerable#each_with_object
代替@slot_formulas[:pm].each do |group|
group.each do |slot_formula|
# <SlotFormula:0x007f81289fae38 id: 8, day: "Monday", doorman: "pm"> ...
end
end
:
map
它有点复杂,但你会获得与额外级别数组相同的输出(以7组为单位)。
输出:
SlotFormula.select(:id, :doorman, :day])
.where(zone_id: 1)
.order(created_at: :asc)
.group_by(&:doorman)
.each_with_object({}) { |(doorman, group), result| result[[doorman]] = group.each_slice(7).to_a }
无论哪种方式,你都可以得到你需要的7个分组,只需使用你填充的那个就可以展示你的桌子。
如何将它们显示为列而非行?
你可以像这样循环结果数组:
{
["am"] => [
[
#<SlotFormula:0x007f81289fb6f8 id: 1, day: "Monday", doorman: "am">,
#<SlotFormula:0x007f81289fb5b8 id: 2, day: "Tuesday", doorman: "am">,
#<SlotFormula:0x007f81289fb478 id: 3, day: "Wednesday", doorman: "am">,
#<SlotFormula:0x007f81289fb338 id: 4, day: "Thursday", doorman: "am">,
#<SlotFormula:0x007f81289fb1f8 id: 5, day: "Friday", doorman: "am">,
#<SlotFormula:0x007f81289fb0b8 id: 6, day: "Saturday", doorman: "am">,
#<SlotFormula:0x007f81289faf78 id: 7, day: "Sunday", doorman: "am">
]
],
["pm"] => [
[
#<SlotFormula:0x007f81289fae38 id: 8, day: "Monday", doorman: "pm">,
#<SlotFormula:0x007f81289facf8 id: 9, day: "Tuesday", doorman: "pm">,
#<SlotFormula:0x007f81289fabb8 id: 10, day: "Wednesday", doorman: "pm">,
#<SlotFormula:0x007f81289faa78 id: 11, day: "Thursday", doorman: "pm">,
#<SlotFormula:0x007f81289fa938 id: 12, day: "Friday", doorman: "pm">,
#<SlotFormula:0x007f81289fa7f8 id: 13, day: "Saturday", doorman: "pm">,
#<SlotFormula:0x007f81289fa6b8 id: 14, day: "Sunday", doorman: "pm">
]
],
["none"] => [
[
#<SlotFormula:0x007f81289fa578 id: 15, day: "Monday", doorman: "none">,
#<SlotFormula:0x007f81289fa438 id: 16, day: "Tuesday", doorman: "none">,
#<SlotFormula:0x007f81289fa2f8 id: 17, day: "Wednesday", doorman: "none">,
#<SlotFormula:0x007f81289fa1b8 id: 18, day: "Thursday", doorman: "none">,
#<SlotFormula:0x007f81289fa078 id: 19, day: "Friday", doorman: "none">,
#<SlotFormula:0x007f81289f9f38 id: 20, day: "Saturday", doorman: "none">,
#<SlotFormula:0x007f81289f9df8 id: 21, day: "Sunday", doorman: "none">
],
[
#<SlotFormula:0x007f81289f9cb8 id: 22, day: "Monday", doorman: "none">,
#<SlotFormula:0x007f81289f9b78 id: 23, day: "Tuesday", doorman: "none">,
#<SlotFormula:0x007f81289f9a38 id: 24, day: "Wednesday", doorman: "none">,
#<SlotFormula:0x007f81289f98f8 id: 25, day: "Thursday", doorman: "none">,
#<SlotFormula:0x007f81289f97b8 id: 26, day: "Friday", doorman: "none">,
#<SlotFormula:0x007f81289f9678 id: 27, day: "Saturday", doorman: "none">,
#<SlotFormula:0x007f81289f9538 id: 28, day: "Sunday", doorman: "none">
]
]
}