I have a model Foo. Foo has many children of type FooChild. FooChild has many FooChildChild. Foo, FooChild and FooChildChild all have and belong to many Bar. I want to create a method or scope on Foo that will return all of the Bar associated with it and it’s child associations. I currently have something like the following on the Foo model.
def foo_child_bars
b = []
self.foo_childs.includes(:bar).collect{|fc| b += fc.bar}
b.uniq
end
def foo_child_child_bars
b = []
self.foo_childs.includes(:foo_child_childs).collect{|fcc| b += fcc.bar}
b.uniq
end
def all_bars
(self.bars + self.foo_child_bars + self.foo_child_child_bars).uniq
end
So now I can call @foo.all_bars and this works and I don’t get duplicates because of the .uniq method.
I imagine that this sort of operation is rather inefficient due to the fact that I’m performing three separate queries. Ideally I’d like to do a single query with a UNION or something so that the database does the heavy lifting of ensuring only unique records are returned and I’m doing as few queries as possible.
I’m also curious if there is a way to use includes() on a nested association such that foo_child_child_bars() could include the :bars which are associated with all of the :foo_child_childs which are being included.
Bar in this reduction represents a table 'images' which has CarrierWave set up on the Image model for managing uploaded images. I’m almost positive that there is a gem out there that would probably simplify this whole thing, but I’d rather just make what I have work and tackle that gem implementation later as it will likely involve a lot of complicated table migrations and refactoring.
two queries
is Bar polymorphic?