Q:

rails: getting most recent record within a loop

I'm trying to get the most recent record if I have a loop. I'm getting an error with this syntax:
<% @user.pictures[0,5].order('created_at DESC').each_with_index do |s, i| %>
  <% link_to ... %>
<% end %>

My error is coming from 
undefined method `order'

What syntax is correct to get the latest record?
Thanks

A:

Try limit:
<% @user.pictures.order('created_at DESC').limit(5).each_with_index do |s, i| %>

@user.pictures[0, 5] returns an array, and array does not have order method which is why you are getting that error.

