1

我有两个模型: Products 和 Tags 通过 products_tags 加入 HABTM 关系。

我目前将我的控制器索引定义为:

@stats = Product.all(:include => :tags).uniq

它返回一个数组。如何返回 Active Rel 对象?我尝试添加作用域,但收到无方法错误。

我需要找到并列出一个唯一的标签列表,并且能够查看每个特定标签所属的产品。

4

1 回答 1

1

试试@stats = Product.includes(:tags).uniq

请注意,uniq将关系转换为数组;如果你想做一些SELECT DISTINCT你想要使用的事情Product.includes(:tags).select('DISTINCT whatever')

于 2012-08-11T00:46:29.453 回答