Forum: Ruby on Rails Best way to collect child rows

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
11e31c35272655b121aa9f8e14d62566?d=identicon&s=25 Jeff (Guest)
on 2008-12-17 21:14
(Received via mailing list)
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?
8fa0f5c2bf8ad238baa4e20cb0360ebd?d=identicon&s=25 Matt Harrison (iwasinnamuknow)
on 2008-12-17 22:17
(Received via mailing list)
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
11e31c35272655b121aa9f8e14d62566?d=identicon&s=25 Jeff (Guest)
on 2008-12-18 00:05
(Received via mailing list)
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 Harrison <iwasinnamuk...@genestate.com>
8fa0f5c2bf8ad238baa4e20cb0360ebd?d=identicon&s=25 Matt Harrison (iwasinnamuknow)
on 2008-12-18 00:18
(Received via mailing list)
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
9a2a53db8e9b4476038c94a64b32833f?d=identicon&s=25 Ryan Bigg (ryan-bigg)
on 2008-12-18 00:29
(Received via mailing list)
def tags
   pages.map(&:items).map(&:tags)
end

If you must.
This topic is locked and can not be replied to.