0

モデルの 1 つにこの named_scope があります。

named_scope :latest_100,
            :from => '(select * from videos order by videos.created_at desc limit 0, 100) as videos'

その目的は、最後の 100 のビデオのプールを作成することです (その結果を「ビデオ」として返すため、他のスコープはそのデータセットで動作します)。その後、スコープをチェーンし、その結果のプールからレコードをフィルター処理できます。

# video.rb
named_scope :most_popular, :order => 'popularity'
named_scope :take_5, :limit => 5

# video_controller.rb
# it gets the latest 100 videos, then sorts those by popularity, and takes the top 5.
@videos = Video.latest_100.most_popular.take_5

Arel に相当するステートメントはありますか?

4

1 に答える 1

0

Arel には .from() メソッドが存在します。

私は次の方法でそれを使用しました:

# video.rb
scope :latest_100, from('(select * from videos limit 100 order by created_at desc) as videos')
scope :most_popular, order('popularity')
scope :take_5, limit(5)

# video_controller.rb
@videos = Video.latest_100.most_popular.take_5

latest_100 は有効な動画のプールを作成し、そこから最も人気のあるトップ 5 を引き出すことができます。

于 2011-09-30T19:52:20.560 に答える