【发布时间】:2014-07-20 20:10:30
【问题描述】:
我正在使用 has_scope gem。
我想确定数据库中文章数组中最后 x 个项目的范围
类似这样的:
scope :last_few_items, -> num { Article.last(num) }
所以在我的查询字符串中我可以说 www.bla.com?last_few_items=2 返回数组的最后两项
但我看到的所有 has_scope 示例都使用以下语法:
scope :scope_name, -> vartopass { where(attribute_val: vartopass)}
'attribute_val' 是我数据库中的所有属性...所以我不太确定如何获得我想要的...除了创建新路由和定义 @articles = Article.last 的新控制器(2) 或类似的东西
【问题讨论】:
标签: ruby-on-rails scope has-scope