Select state city rails4

Asked

Viewed 272 times

0

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

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

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

recalling that the capital is an attribute of the integer state table

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")

Browser other questions tagged

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