# Best way to collect child rows

**URL:** <https://www.ruby-forum.com/t/best-way-to-collect-child-rows/155875>\
**Category:** Rails\
**Created:** [December 17, 2008, 9:14pm UTC](https://www.ruby-forum.com/t/best-way-to-collect-child-rows/155875 "2008-12-17T21:14:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [December 17, 2008, 9:14pm UTC](https://www.ruby-forum.com/t/best-way-to-collect-child-rows/155875/1 "2008-12-17T21:14:04Z")

</div>

I’m trying to find the best way to handle this task, and I’m not  
having much luck. I think I lack knowledge of the terminology to  
research this. I have these models:

Account, which has many Pages  
Page, has many Items  
Item, has many Tags (through a join model: ItemTag)  
Tag, has many Items (through same join model)

What’s the simplest way to retrieve all Tags associated with an  
Account? Right now, the best I can do is iterate through each Item and  
add the associated tags to an array. This seems overly cumbersome, and  
I’m hoping there’s a better way. Any advice?

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [December 17, 2008, 10:17pm UTC](https://www.ruby-forum.com/t/best-way-to-collect-child-rows/155875/2 "2008-12-17T22:17:43Z")

</div>

Jeff wrote:

> Account? Right now, the best I can do is iterate through each Item and  
> add the associated tags to an array. This seems overly cumbersome, and  
> I’m hoping there’s a better way. Any advice?

I think you’re looking for has\_and\_belongs\_to\_many association. Try  
something like:

#app/models/item.rb  
has\_and\_belongs\_to\_many :tags

#app/models/tag.rb  
has\_and\_belongs\_to\_many :items

#app/models/item\_tag.rb  
has\_many :tags  
has\_many :items

I haven’t tested that but it should do what you want.

HTH

Matt

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [December 18, 2008, 12:05am UTC](https://www.ruby-forum.com/t/best-way-to-collect-child-rows/155875/3 "2008-12-18T00:05:37Z")

</div>

What I have now is similar, but using has\_many :through instead of  
habtm. What I’m looking for is something functionally equivalent to:

account = Account.find(:first)  
account.tags # returns all tags across all items, on all pages

Note that the tags are actually associated with items, which are  
associated with pages, which are associated with accounts. A few  
degrees of separation there. Will switching to habtm allow this?

Thanks,  
Jeffr

On Dec 17, 1:17 pm, Matt H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [December 18, 2008, 12:29am UTC](https://www.ruby-forum.com/t/best-way-to-collect-child-rows/155875/4 "2008-12-18T00:29:06Z")

</div>

def tags  
pages.map(&:items).map(&:tags)  
end

If you must.

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [December 18, 2008, 12:18am UTC](https://www.ruby-forum.com/t/best-way-to-collect-child-rows/155875/5 "2008-12-18T00:18:33Z")

</div>

Jeff wrote:

> What I have now is similar, but using has\_many :through instead of  
> habtm. What I’m looking for is something functionally equivalent to:
> 
> account = Account.find(:first)  
> account.tags # returns all tags across all items, on all pages
> 
> Note that the tags are actually associated with items, which are  
> associated with pages, which are associated with accounts. A few  
> degrees of separation there. Will switching to habtm allow this?

Not that I’m aware of. Sorry but I think I misread the critial part of  
your post. As far as I know (and some ruby genius will come up with a  
far better idea) the only way to do what you want is to iterate through  
the pages and the items and then collect tags.

It might be possible to do it with an association but I don’t know how,  
although I would be interested to know if it is possible.

> Thanks,  
> Jeffr

Matt
