我需要根据 created_at 列按年和月对用户进行分组,
User.all.group_by{ |q| [q.created_at.year, q.created_at.month]}
,
在我使用键 [year, month] 获取哈希的地方,有什么方法可以对结果如
{
year1 =>{ month1 =>[array of records], month2=>[array]},
year2 =>{ month1 =>[array of records], month2=>[array]}
}
最佳答案
尝试以下操作:
User.all
.group_by { |user| user.created_at.year }
.transform_values { |users| users.group_by { |user| user.created_at.month } }
关于ruby-on-rails - 按 ruby on rails 分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49512938/