Sort array in rails [closed]

0

I can not sort my array in descending order of id.

<%@vendas = Vendas.find(:all,:order=>"id, DESC")%>
    
asked by anonymous 22.02.2016 / 15:21

2 answers

1

Friend, I do not know how your code works, because the find method requires the ID of the object you want to redeem.

Try to use the new Ruby syntax instead of the 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, as it is not good practice to leave the view responsible for tasks like these.

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

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

    
23.02.2016 / 18:26
0

Correct method:

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