# Habtm on an array (or object.habtm.habtm)

**URL:** <https://www.ruby-forum.com/t/habtm-on-an-array-or-object-habtm-habtm/95410>\
**Category:** Rails\
**Created:** [May 31, 2006, 2:27pm UTC](https://www.ruby-forum.com/t/habtm-on-an-array-or-object-habtm-habtm/95410 "2006-05-31T14:27:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ec8ea/32.png) [@daniel](https://www.ruby-forum.com/u/daniel)\
**Post date:** [May 31, 2006, 2:27pm UTC](https://www.ruby-forum.com/t/habtm-on-an-array-or-object-habtm-habtm/95410/1 "2006-05-31T14:27:24Z")

</div>

Hi,

I have three Models: User, Group and File.

User habtm groups  
and  
Group habtm Files

How can I find all files that a particular user has access to?  
eg user.groups.files ?

As user.groups returns an array, the above doesn’t work.

Id like to find the files for all groups in that array, without adding  
any code to the User model.

Thanks in advance

PS I just posted this in the ‘Ruby’ Forum by accident, so sorry about  
the double post…

---

<div class="post-metadata">

**Author:** ![daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ec8ea/32.png) [@daniel](https://www.ruby-forum.com/u/daniel)\
**Post date:** [May 31, 2006, 3:07pm UTC](https://www.ruby-forum.com/t/habtm-on-an-array-or-object-habtm-habtm/95410/2 "2006-05-31T15:07:31Z")

</div>

Hi Daniel,  
I think you must create a method User#files which should look like  
this :

class User \< ActiveRecord::Base  
# …rest…  
def groups\_files  
groups.map {|g| g.files }.flatten  
end  
# …rest…  
end

Hope it helps :-).

Le 31 mai 06 à 14:27, Daniel a écrit :

---

<div class="post-metadata">

**Author:** ![daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ec8ea/32.png) [@daniel](https://www.ruby-forum.com/u/daniel)\
**Post date:** [May 31, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/habtm-on-an-array-or-object-habtm-habtm/95410/3 "2006-05-31T18:56:09Z")

</div>

Roman LE NEGRATE wrote:

> Hi Daniel,  
> I think you must create a method User#files which should look like  
> this :
> 
> class User \< ActiveRecord::Base
> 
> # …rest…
> 
> def groups\_files  
> groups.map {|g| g.files }.flatten  
> end
> 
> # …rest…
> 
> end

That will work, but when you do that, you probably want an :include =\>  
:files on the groups finder so that you don’t have a separate database  
query for every group the user has.

```
def groups_files
	groups.find(:all, :include => :files).map(&:files).flatten.uniq
end

```

The map(&:files) is just a shorter way of saying what Roman said with  
the other syntax, and the uniq method removes duplicates.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ec8ea/32.png) [@daniel](https://www.ruby-forum.com/u/daniel)\
**Post date:** [May 31, 2006, 8:22pm UTC](https://www.ruby-forum.com/t/habtm-on-an-array-or-object-habtm-habtm/95410/4 "2006-05-31T20:22:01Z")

</div>

Le 31 mai 06 à 18:56, Josh S. a écrit :

> That will work, but when you do that, you probably want an :include =\>  
> :files on the groups finder so that you don’t have a separate database  
> query for every group the user has.
> 
> def groups\_files  
> groups.find(:all, :include =\> :files).map(&:files).flatten.uniq  
> end
> 
> The map(&:files) is just a shorter way of saying what Roman said with  
> the other syntax, and the uniq method removes duplicates.

Thank you Josh. I did not know the map(&:method\_id) trick. Does it  
come with Ruby \> 1.8.2 ? I could not find its documentation in the  
PickAxe2 book. Could you please link us to informations about it  
?\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

---

<div class="post-metadata">

**Author:** ![daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ec8ea/32.png) [@daniel](https://www.ruby-forum.com/u/daniel)\
**Post date:** [June 1, 2006, 7:35pm UTC](https://www.ruby-forum.com/t/habtm-on-an-array-or-object-habtm-habtm/95410/5 "2006-06-01T19:35:10Z")

</div>

> It’s not standard Ruby yet, thought I think it’s on the way in a  
> future  
> release. But as of Rails 1.1, it’s part of ActiveSupport. I was as  
> surprised as anyone when I first discovered it:
> 
> [http://blog.hasmanythrough.com/articles/2006/03/07/symbol-to-proc-](http://blog.hasmanythrough.com/articles/2006/03/07/symbol-to-proc-)  
> shorthand

Good ! Thanks.

---

<div class="post-metadata">

**Author:** ![daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ec8ea/32.png) [@daniel](https://www.ruby-forum.com/u/daniel)\
**Post date:** [May 31, 2006, 11:36pm UTC](https://www.ruby-forum.com/t/habtm-on-an-array-or-object-habtm-habtm/95410/6 "2006-05-31T23:36:05Z")

</div>

Roman LE NEGRATE wrote:

> Thank you Josh. I did not know the map(&:method\_id) trick. Does it  
> come with Ruby \> 1.8.2 ? I could not find its documentation in the  
> PickAxe2 book. Could you please link us to informations about it

It’s not standard Ruby yet, thought I think it’s on the way in a future  
release. But as of Rails 1.1, it’s part of ActiveSupport. I was as  
surprised as anyone when I first discovered it:

[http://blog.hasmanythrough.com/articles/2006/03/07/symbol-to-proc-shorthand](http://blog.hasmanythrough.com/articles/2006/03/07/symbol-to-proc-shorthand)

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ec8ea/32.png) [@daniel](https://www.ruby-forum.com/u/daniel)\
**Post date:** [June 10, 2006, 11:23pm UTC](https://www.ruby-forum.com/t/habtm-on-an-array-or-object-habtm-habtm/95410/7 "2006-06-10T23:23:40Z")

</div>

If someone is interested in the speeds difference between obj.map {|  
o| o.method\_name } and obj.map(&:method\_name) , I commented  
Josh’s article with a benchmark and its results. It’s readable right  
at: [http://blog.hasmanythrough.com/articles/2006/03/07/symbol-to-proc-](http://blog.hasmanythrough.com/articles/2006/03/07/symbol-to-proc-)  
shorthand#comment-324

Kind regards,  
Roman
