2013-02-19 17 views

回答

2

scoped返回匿名範圍。從API文檔:

當程序生成複雜查詢時,匿名範圍往往很有用,因爲在第一類對象周圍傳遞中間值(範圍)時非常方便。

這裏的例子:

posts = Post.scoped 
posts.size # Fires "select count(*) from posts" and returns the count 
posts.each {|p| puts p.name } # Fires "select * from posts" and loads post objects 

fruits = Fruit.scoped 
fruits = fruits.where(:color => 'red') if options[:red_only] 
fruits = fruits.limit(10) if limited?