How do I order database records in rails by most recent?

Viewed 12031

I want to order all the items in a model Item so it displays the most recent ones first. According to the Rails guide, the following code should work:

 Item.order("created_at DESC")

However, when I type that (or varieties) in the terminal, the most recent item always shows up last, and that's how they show up on my page. How do I efficiently retrieve them with he most recent first? (I'm going to display only some of them at a time on each page.)

Note that my default scope for items is oldest first.

Update: This is the SQL I get:

SELECT "comments".* FROM "comments" ORDER BY comments.created_at ASC, created_at DESC

So I guess I shouldn't use default scopes...

6 Answers

Correct one and tested

@purchase_orders = current_company.purchase_orders.order(:date)
@purchase_orders = @purchase_orders.reverse_order 
Related