0

I'm using the has_scope gem.

I want to scope the last x number of items in an array of Articles in my database

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

but all the examples I see for has_scope use this syntax:

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

infused
  • 24,000
  • 13
  • 68
  • 78
rikkitikkitumbo
  • 954
  • 3
  • 17
  • 38

1 Answers1

0

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

Did you tried it? This is actually the right way to do it, Rails natively implement last(n)

Benjamin Bouchet
  • 12,971
  • 2
  • 41
  • 73
  • yep. That works... I was just wondering if there was a way to filter it in the querystring, without having to make a new route for some strange list. – rikkitikkitumbo Jul 20 '14 at 20:51
  • Well, you need to get the data (i.e. how many records) from somewhere right? if you are doing a get request it is necessary in your query-string. Or you perhaps implement a complex thing with your javascript updating a cookie on your client side so you keep your url clean. But some people (rare, but still) are disabling cookies – Benjamin Bouchet Jul 20 '14 at 21:06