# Find all by a different association id

**URL:** <https://www.ruby-forum.com/t/find-all-by-a-different-association-id/186616>\
**Category:** Rails\
**Created:** [April 9, 2010, 8:27pm UTC](https://www.ruby-forum.com/t/find-all-by-a-different-association-id/186616 "2010-04-09T20:27:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ec9cab/32.png) [@brent](https://www.ruby-forum.com/u/brent)\
**Post date:** [April 9, 2010, 8:27pm UTC](https://www.ruby-forum.com/t/find-all-by-a-different-association-id/186616/1 "2010-04-09T20:27:18Z")

</div>

I have 3 models here setup like thus:

AttachmentType  
has\_many :attachments

Attachment  
belongs\_to :page  
belongs\_to :attachment\_type

Page  
has\_many :attachments

Attachment belongs to both an AttachmentType & Page.

How can I find all AttachmentType by a page id?

I suppose I could say that AttachmentType has\_one :page, but that  
seems a bit unnecessary.

---

<div class="post-metadata">

**Author:** ![brent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ec9cab/32.png) [@brent](https://www.ruby-forum.com/u/brent)\
**Post date:** [April 9, 2010, 8:48pm UTC](https://www.ruby-forum.com/t/find-all-by-a-different-association-id/186616/2 "2010-04-09T20:48:23Z")

</div>

On 9 April 2010 19:25, Brent [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> has_many :attachments
> 
> ```
> 
> How can I find all AttachmentType by a page id?

Page  
has\_many :attachments  
has\_many :attachment\_types :through =\> :attachments

---

<div class="post-metadata">

**Author:** ![brent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ec9cab/32.png) [@brent](https://www.ruby-forum.com/u/brent)\
**Post date:** [April 12, 2010, 5:52pm UTC](https://www.ruby-forum.com/t/find-all-by-a-different-association-id/186616/3 "2010-04-12T17:52:58Z")

</div>

Hmm… OK I did this and I can grab attachment\_types through a page.  
BUT I can’t grab the specific attachments in the attachment\_types in a  
page… for instance if I try this:

@types = @page.attachment\_types  
\<% @types.each do |t| %\>  
\<%= render(:partial =\> ‘partial/attachment’, :collection =\>  
t.attachments) %\>  
\<% end %\>

It looks like when I grab attachments from the type, it grabs ALL  
attachments from that type, not attachments specifically associated to  
that page. How can I tell it to do a :through, with a model that isn’t  
associated in the AttachmentType model?

---

<div class="post-metadata">

**Author:** ![brent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ec9cab/32.png) [@brent](https://www.ruby-forum.com/u/brent)\
**Post date:** [April 12, 2010, 6:01pm UTC](https://www.ruby-forum.com/t/find-all-by-a-different-association-id/186616/4 "2010-04-12T18:01:10Z")

</div>

OK I think I found the solution:

@types = @page.attachment\_types

\<% @types.each do |t| %\>  
\<%= render(:partial =\> ‘partial/attachment’, :collection =\>  
t.attachments.find\_all\_by\_page\_id(@page.id)) %\>  
\<% end %\>

didn’t know I could still call the find\_all\_by\_page\_id after I called  
it from a model.
