# Through and through

**URL:** <https://www.ruby-forum.com/t/through-and-through/74212>\
**Category:** Rails\
**Created:** [November 1, 2006, 11:27pm UTC](https://www.ruby-forum.com/t/through-and-through/74212 "2006-11-01T23:27:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [November 1, 2006, 11:27pm UTC](https://www.ruby-forum.com/t/through-and-through/74212/1 "2006-11-01T23:27:35Z")

</div>

I have users, groups, permissions, and volumes, with these  
relationships:

- users HABTM groups

- groups has\_many permissions

- permissions has\_many volumes

- groups has\_many volumes :through =\> permissions

Is there an easy way to get all the volumes a user has access to? I can  
always get a list of groups, loop over it, and get the volumes, but I  
was hoping for something elegant like user.groups.volumes.

Possible?

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [November 2, 2006, 8:31pm UTC](https://www.ruby-forum.com/t/through-and-through/74212/2 "2006-11-02T20:31:57Z")

</div>

Brian A. wrote:

> Is there an easy way to get all the volumes a user has access to? I can  
> always get a list of groups, loop over it, and get the volumes, but I  
> was hoping for something elegant like user.groups.volumes.
> 
> Possible?

Perhaps something like user.groups[0].volumes would work.

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [November 2, 2006, 9:19pm UTC](https://www.ruby-forum.com/t/through-and-through/74212/3 "2006-11-02T21:19:31Z")

</div>

Brian A. wrote:

> I have users, groups, permissions, and volumes, with these  
> relationships:
> 
> - users HABTM groups
> 
> - groups has\_many permissions
> 
> - permissions has\_many volumes
> 
> - groups has\_many volumes :through =\> permissions
> 
> Is there an easy way to get all the volumes a user has access to? I can  
> always get a list of groups, loop over it, and get the volumes, but I  
> was hoping for something elegant like user.groups.volumes.
> 
> Possible?

@user.groups.map{|g| g.volumes}.flatten

OR, the DRY way:

class Users \< ActiveRecord::Base  
def volumes  
self.groups.map{|g| g.volumes}.flatten  
end

Then you can call @user.volumes.

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [November 2, 2006, 9:42pm UTC](https://www.ruby-forum.com/t/through-and-through/74212/4 "2006-11-02T21:42:30Z")

</div>

Brian A. wrote:

> Is there an easy way to get all the volumes a user has access to? I can  
> always get a list of groups, loop over it, and get the volumes, but I  
> was hoping for something elegant like user.groups.volumes.
> 
> Possible?

Maybe something like this:

@volumes = []  
user.groups.each { |g| @volumes += g.volumes }

You can try to make it more efficient by first querying @groups =  
user.groups.find(:all, :include =\> :volumes)

Although I’m not sure what the status is of eager loading of :through  
relationships.

I assume you are doing something with permissions that would limit what  
you could do with a volume, so you could conceivable end up with  
multiple instances of a volume, each with different permissions. How  
would you resolve that?

-matthew

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [November 3, 2006, 7:38am UTC](https://www.ruby-forum.com/t/through-and-through/74212/5 "2006-11-03T07:38:46Z")

</div>

> I assume you are doing something with permissions that would limit what  
> you could do with a volume, so you could conceivable end up with  
> multiple instances of a volume, each with different permissions. How  
> would you resolve that?
> 
> -matthew

Fill an array with all volumes, then call uniq! to remove dupes.

Thanks for all the responses.
