I guess we do not need to write so complex code. Maybe it can be simpler like(not accurate):
```ruby
UserGroup.all.each do |group|
    rs[group.name] = group.users.map{|user| user.name}
```