[英]Using rails has_scope gem to scope the last x number of items in array
I'm using the has_scope gem. 我正在使用has_scope gem。
I want to scope the last x number of items in an array of Articles in my database 我想在数据库中的文章数组中确定最后x个项目的范围
something like this: 像这样的东西:
scope :last_few_items, -> num { Article.last(num) }
so in my querystring I could say www.bla.com?last_few_items=2 to return the last 2 items of an array 所以在我的查询字符串中,我可以说www.bla.com?last_few_items=2返回数组的最后2个项目
but all the examples I see for has_scope use this syntax: 但是我为has_scope看到的所有示例都使用以下语法:
scope :scope_name, -> vartopass { where(attribute_val: vartopass)}
wher 'attribute_val' are all attributes from my database... so I'm not really sure how to get what I want... aside from making a new route and a new controller that defines @articles = Article.last(2) or something like that wher“attribute_val”是从我的数据库中的所有属性,所以,我真的不知道如何得到我想要的东西......除了制作新路线和新的控制器,它定义@articles = Article.last(2)或类似的东西
This is extracted from your question: 这是从您的问题中提取的:
wher 'attribute_val' are all attributes from my database... so I'm not really sure how to get what I want... aside from making a new route and a new controller that defines @articles = Article.last(2) or something like that wher“attribute_val”是从我的数据库中的所有属性,所以,我真的不知道如何得到我想要的东西......除了制作新路线和新的控制器,它定义@articles = Article.last(2)或类似的东西
Did you tried it? 你试过了吗? This is actually the right way to do it, Rails natively implement last(n)
这实际上是做正确的方式,导轨本身执行last(n)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.