Answer the question
In order to leave comments, you need to log in
How to gracefully perform a single sort in RoR ( AR )?
Evening in joy, seagulls in sweetness.
ENV: ruby 2.4.1 + ror 5.1.4 + postgresql
Model available:
create_table "phones", force: :cascade do |t|
t.integer "id"
t.string "type"
t.datetime "created_at"
end
ordering_by_id = []
ordering_by_id << Phone.where(type: 'ios').order(created_at: :desc).select(:id) ....
ordering_by_id << Phone.where(type: 'android').order(created_at: :desc).select(:id) ....
@phones = Phone.where(id: ordering_by_id).order_as_specified(id: ordering_by_id)
Answer the question
In order to leave comments, you need to log in
Store the type like a rails enum. The field in base will be integer. Specify the desired order in the model.
Theoretically it should work like this
SELECT *
FROM phones
ORDER BY idx(array['ios','android','wp'], phones.type)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question