The project uses the friendly_id gem on the name field. How to sort the query? If you do something like: Model.all.order(name: :asc) , then the sorting is ultimately across the slug field, but since name in Russian there is a problem.

Update

model job.rb:

  extend FriendlyId friendly_id :name, use: [:slugged, :finders] 

model example.rb:

  belongs_to :job 

I'm trying to do something like this:

  ExampleJob.all.includes(:job).order("LOWER('jobs.name') DESC") 

I also tried:

  Job.where(id: ids_arr).order(name: :desc) 

Logs:

 Job.all.order(name: :desc) Job Load (2.5ms) SELECT "jobs".* FROM "jobs" ORDER BY "jobs"."name" DESC => [#<Job:0x007fd316133558 id: 2, name: "Диагностика", created_at:..., slug: "diagnostika">, #<Job:0x007fd316133328 id: 3, name: "Установка", created_at: ..., slug: "ustanovka">, #<Job:0x007fd3161330a8 id: 1, name: "Замена", created_at: ..., slug: "zamena">] 

Ie the name should have been: Diagnostics, Replacement, Installation, obviously all the same here sorted by slug

PS postgresql 9.3 db

 Job.all.order(name: :desc).explain Job Load (0.9ms) SELECT "jobs".* FROM "jobs" ORDER BY "jobs"."name" DESC => EXPLAIN for: SELECT "jobs".* FROM "jobs" ORDER BY "jobs"."name" DESC QUERY PLAN -------------------------------------------------------------- Sort (cost=51.37..53.17 rows=720 width=84) Sort Key: name DESC -> Seq Scan on jobs (cost=0.00..17.20 rows=720 width=84) (3 rows) 
  • There should be no slug sorting, unless you explicitly indicate this, for example in the model. Will you be able to supplement the question with code from the model with which you connect FriendlyId to it? - Vitaly Emelyantsev
  • @ VitaliyEmeliantsev updated - Tiazar
  • What kind of database do you have? Because the SQL query is definitely correct, and sorting the data after unloading from the database is an extremely rash step for friendly_id, it is unlikely that it is doing so. - D-side
  • @ D-side specifically in this task I don’t need friendly_id at all, but it is used in other parts of the project, postgresql 9.3 DB - Tiazar
  • Show the output of Job.all.order(name: :desc).pluck(:name) - D-side

0