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> wrote: > Jeff wrote: > > 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? > > 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---