我正在使用活跃的记录信誉系统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})
但我不知道如何获得相关的投票。有什么想法吗?
答案 0 :(得分:2)
这两个可能是可链接的:
@businesses = @estate.
businesses.
joins(:categories).
where(categories: {id: @category.id}).
find_with_reputation(:votes, :all, order: "votes desc")
为什么这可以工作?:ActiveRecord中的大多数查询实际上不返回对象,而是一个ActiveRecord::Relation
对象,您可以将查询附加到并用于进一步的链式查询。