Sort array in Rails

Asked

Viewed 571 times

0

I cannot sort my array in descending id order.

<%@vendas = Vendas.find(:all,:order=>"id, DESC")%>

2 answers

1

Friend, I don’t know how your code works, because the find method requires the ID of the object you want to rescue.

Try using the new Ruby syntax instead of rockets. Also try to use spacing to make your code more readable.

The correct code to do what you want is the following:

<% @vendas = Vendas.order("id DESC") %>

Another thing, I recommend moving this line of code to a controller, because it’s not good practice to leave the vision responsible for tasks like these.

In your controller you could do: @ultimas_vendas = Vendas.order("id DESC") And in the view just call @ultimas_sales.

It is important to note that an array is not returned, but an Activerecord_relation.

0

Correct method:

<%@vendas = Vendas.find(:all,:order=>"id DESC")%>
  • 3

    That’s the answer?

  • @Math seems to me yes, the difference is the comma.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.