How can I create a query with alternating sorting in rails 5?

0

I have the following formatting in my database

Products
 - Name
 - Store Id

with the following records

product 1 - Shop 1
product 2 - Shop 1
product 3 - Shop 3
product 4 - Shop 1
product 5 - Shop 2
product 6 - Shop 2
product 7 - Shop 1
product 8 - Shop 1
product 9 - Shop 3
product 10 - Shop 3

I want to do a query where I bring the result by switching to the "Store Id", something that looks like this:

product 1 - Shop 1
product 5 - Shop 2
product 3 - Shop 3
product 2 - Shop 1
product 6 - Shop 2
product 9 - Shop 3
product 4 - Shop 1
product 10- Shop 3
product 7 - Shop 1
product 8 - Shop 1

This listing will still be paged.

    
asked by anonymous 07.11.2017 / 13:21

1 answer

0

Just use the "order" method your model already inherits from ActiveRecord:

Produto.order("loja_id asc, nome asc")
    
07.11.2017 / 14:07