Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - take(4) but the bottom not the top?

I have @comments which lets say has 17 records

What I want is the last 4: 14,15,16,17

I tried:

<% @comments.take(4).each do |comment| %>

But that returns the first 4.. How can I grab the last 4 comments, not the first 4

Thanks

like image 910
AnApprentice Avatar asked Dec 03 '22 03:12

AnApprentice


2 Answers

You could also do:

@comments.last(4)...
like image 114
coder_tim Avatar answered Dec 30 '22 02:12

coder_tim


<% @comments.reverse.take(4).each do |comment| %>
like image 30
Don Roby Avatar answered Dec 30 '22 04:12

Don Roby