Search code examples
ruby-on-railsproduction

rails .order desc not working in production


It is very wierd that the order is working in local but when i push it to heroku its not displaying the correct order

<% @address.address.cases.order(due_date: :desc).where.not(invoice_file_name: nil).last(10).each do |d|  %> 

        <ul id="inv"><p><%= link_to d.invoice.url do %>
         <li> <%= d.pt_first_name.capitalize %> <%= d.pt_last_name.capitalize %></li>
        <% end %></p></ul>
      <% end %>

Solution

  • I suppose the reason is the last method is forces the ascending order. Take a look at your logs. You should see something similar:

    ORDER BY `cases`.`due_date` ASC LIMIT 10
    

    To fix this I'd use the limit method:

    @address.address.cases.order(due_date: :desc).where.not(invoice_file_name: nil).limit(10)
    

    It uses the proper order:

    ORDER BY `cases`.`due_date` DESC LIMIT 10