0

Railsで音楽を販売できるサイトを作りました。音楽の売り上げに基づいて、その週の最も人気のあるアーティストを返したいと思っています。以下は、モデル関係の簡単な概要です。

モデル

# Artist
attr_accessible :name, :website etc.

has_many :songs, :through => :artist_song
has_many :artist_song

# Cart

attr_accessible :purchased_at, :user_id, :id, :songs, :cart_song
      
has_many :songs, :through => :cart_song
has_many :cart_song

# Song

has_many :carts, :through => :cart_song
has_many :cart_song
      
has_many :artists, :through => :artist_song
has_many :artist_song

以下のコードを使用して、過去 'x' 日間に販売されたすべての曲を取得できますが、これらの結果内でグループ化またはカウントを実装する方法がわかりません。

Song.find(:all, :conditions => ['carts.purchased_at IS NOT NULL AND carts.purchased_at >= ?', 7.days.ago.at_midnight], :include => :carts)

一意の属性で何らかのグループ化またはカウントを行うと思いますが、ここから立ち往生しているので、どんな助けも大歓迎です。ありがとう。

4

1 に答える 1

0
Song.select('?').where(
  'carts.purchased_at IS NOT NULL AND carts.purchased_at >= ?',
  7.days.ago.at_midnight
  ).includes(:carts).group('?')

どこ ?グループ化したい属性です。最終的に、グループごとに 1 つのレコードが返されます。

于 2012-04-17T17:07:25.137 に答える