Select state city rails4

0

I have a dynamic select of states and cities and my idea is to list the state capital first selected.

I have the following code to list the cities but that is not listing the capital first:

@cidades = Cidade.where("estado_id = ?", Estado.first.id).order(id: Estado.select(:capital))

remembering that the capital is an attribute of the state table of type integer

    
asked by anonymous 07.11.2016 / 03:45

1 answer

0

To sort by specific values you have to use a case.

@cidades = Cidade.where("estado_id = ?", Estado.first.id).order("CASE id
WHEN #{Estado.select(:capital).id} THEN 0 ELSE 1 END")
    
07.11.2016 / 14:13