user12763413
user12763413

Reputation: 1349

Case insensitive ordering for citext field type in rails

Migration File.

class ChangeDataTypeOrganisationName < ActiveRecord::Migration[5.1]
  def up
    enable_extension :citext
    change_column :organisations, :name, :citext
  end
  
  def down
    change_column :organisations, :name, :text
    disable_extension :citext
  end
end

index.json.jbuilder

json.offices_filters @offices.order(:name) do |office|
  json.(office, :id, :name)
end

I want to order the offices_filter based on the offices name. I had field type for office name as text so just to make it case-insensitive I changed the data type to citext just to perform case-insensitve ordering. But the ordering is still case-sensitive. First offices are orders which lower case and then upper case officers are ordered. Please help me resolve this issue.

Update 1

PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "offices"
LINE 1: ...$1) ORDER BY "organisations"."updated_at" DESC, LOWER(offices.na...

Update 2

organisations_controller.rb

def index
 @offices = Organisation.all
 @offices = offices.order(updated_at: :desc)
end

Update 3

Ankor
asdas
John Wilson
Abp
Sim Limited

Upvotes: 0

Views: 363

Answers (1)

Alter Lagos
Alter Lagos

Reputation: 12550

Maybe you could try with:

@offices.order('LOWER(organisations.name)')

Upvotes: 1

Related Questions