Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 什么';s ActiveRelation(arel)等价于:from_Ruby On Rails_Ruby On Rails 3_Activerecord_Rails 3 Upgrade_Active Relation - Fatal编程技术网

Ruby on rails 什么';s ActiveRelation(arel)等价于:from

Ruby on rails 什么';s ActiveRelation(arel)等价于:from,ruby-on-rails,ruby-on-rails-3,activerecord,rails-3-upgrade,active-relation,Ruby On Rails,Ruby On Rails 3,Activerecord,Rails 3 Upgrade,Active Relation,我在我的一个模型中有一个命名的_范围: named_scope :latest_100, :from => '(select * from videos order by videos.created_at desc limit 0, 100) as videos' 它的目的是创建一个包含最后100个视频的池(将该结果作为“视频”返回,以便其他作用域在该数据集上操作),然后我可以在此之后链接作用域,并从该结果池中过滤记录 # video.rb named_scop

我在我的一个模型中有一个命名的_范围:

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是否有等效语句?

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
最新的_100将创建一个有效的视频池,您可以从中提取前5名最受欢迎的视频