2

我正在使用活动记录信誉系统 gem。

我的出发点是显示带有相关投票的企业列表:

@businesses = Business.find_with_reputation(:votes, :all, order: "votes desc")

现在我需要按类别和资产对业务进行分组。

我想显示庄园内某个类别的企业及其相关投票的列表。

所以我有以下模型(多对多)

Business has many Categories through Categorizations
Category has many Businesses through Categorizations

Business has many Estates through Localizations
Estate has many Businesses through Localizations

我可以得到分组的业务

@businesses = @estate.businesses.joins(:categories).where(categories: {id: @category.id})

但我不知道如何获得相关选票。有任何想法吗?

4

1 回答 1

2

两者可能是可链接的:

@businesses = @estate.
                businesses.
                joins(:categories).
                where(categories: {id: @category.id}).
                find_with_reputation(:votes, :all, order: "votes desc")

为什么会这样?:ActiveRecord 中的大多数查询实际上不返回对象,而是一个ActiveRecord::Relation可以附加查询并用于进一步链接查询的对象。

于 2013-11-11T13:35:26.150 回答