我有一个模型用户,它有 id、name、surname、role_id、permission_id
这是一些用户条目:
User => {:name => 'Bob', :surname => 'surnmae', :role_id => 1, :permission_id = 2}
User => {:name => 'Alice', :surname => 'strange', :role_id => 1, :permission_id = 3}
User => {:name => 'Ted', :surname => 'Teddy', :role_id => 2, :permission_id = 3}
现在我需要先将它们分组role_id
,然后再分组permission_id
,这就是我的意思:
Category.select([:name, :role_id, :permission_id]).group_by(&:role_id)
生产:
{1 =>
[#<User name: "Bob", role_id: 1, permission_id: 2>,
#<User name: "Alice", role_id: 1, permission_id: 3>]
2 => [#<User name: "Ted", role_id: 2, permission_id: 3>]
}
这足够接近,但我需要将它们与 permission_id 分组,所以它看起来像这样:
{:role_id => {:permision_id => [Array of users grouped by this criteria]}
}