【发布时间】:2012-03-27 08:31:33
【问题描述】:
我有两个关联的模型。我有一个 Artist 模型和一个 Review 模型。
class Artist
include DataMapper::Resource
property :id, Serial
property :name, String
has n, :reviews
end
class Review
include DataMapper::Resource
property :id, Serial
property :rating, Integer
property :body, String
belongs_to :artist
end
我的目标是检索一组按与他们相关的评论的平均评分排序的艺术家。我不确定是否有任何单一的解决方案,但我目前的方法是创建一个数组,其中包含按平均评分排序的艺术家的 id。我如何获得这个数组的细节对于这个问题并不重要,但假设我现在有一个数组。
ids_sorted_by_rating = [4,5,23,9,2,48,17,....]
然后我检索艺术家。
artists = Artist.all(:id => ids_sorted_by_rating)
问题在于 datamapper 按 :id 排序,除非您指定其他选项。因此,在我为获得 ids_sorted_by_rating 付出了所有努力之后,我仍然得到了一个按 :id 排序的艺术家列表。
任何能够给我一个按相关评论的平均评分排序的艺术家列表的解决方案都将不胜感激。如果我不需要访问数据库一百万次即可获得该列表,则可以加分。 :)
谢谢,如果您需要更多信息,请询问,我会提供!
【问题讨论】:
标签: ruby associations datamapper