Forum: Ruby on Rails Using through with habtm

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.
A7f6b372a89349258d08a4d7acb186d4?d=identicon&s=25 Serengeti (Guest)
on 2007-06-22 04:51
(Received via mailing list)
I have two models with an habtm relationship:

Foo has_and_belongs_to_many Bars (and vice-versa, obviously).

Let's say Bar has_many Bazs (how the heck do you pluralize Baz?).

It would be nice to be able to say that Foo has_many Bazs through =>
Bar, but this does not seem to work.

Am I right, or should it be possible to do this?

The best I have been able to do is this:

    bazs = []
    my_foo.bars.each do |b|
      bazs += b.bazs
    end
    bazs.uniq!

That's not TOO messy, but not as clean as foo.bazs.

If I am right about foo.bazs not being possible, is there a better/
cleaner workaround than the one I am using?
This topic is locked and can not be replied to.