像这样工作:
#Get all Foos @foos = @goos.map(&:foo).compact.uniq #Get all Boos @boos = @goos.map(&:foo).map(&:boo).compact.uniq
虽然它不是很好,但它返回一个数组而不是一个活动的记录关系,这意味着我无法调用 where 和 order 在上面。
where
order
编辑:
如果由于某种原因保留活动记录关系类很重要,这是更好的方法:
#Get all Foos @foos = Foo.where(id: @goos.map(&:foo.id).uniq) #Get all Boos @boos = Boo.where(id: @goos.map(&:foo).map(&:boo_id).uniq)