rubyransackmeta-search

Ransack custom sorting not works


I can't create custom sort for ransack(by scope). Tried to do as the tutorial says, but it not successful.

I have next code in my Model.

scope :sort_by_custom_name_asc, -> { order('"core_people"."phone" ASC') }

Proofs that scope works, sort by phone in ransack works, but custom_name sort don't.

Core::Person.ransack(s: 'custom_name asc').result.to_sql
#=> "SELECT \"core_people\".* FROM \"core_people\""

Core::Person.ransack(s: 'phone asc').result.to_sql
#=> "SELECT \"core_people\".* FROM \"core_people\" ORDER BYY\"core_people\".\"phone\" ASC"

Core::Person.sort_by_custom_name_asc.to_sql
#=> "SELECT \"core_people\".* FROM \"core_people\" ORDER BY \"core_people\".\"phone\" ASC"

Solution

  • I found another way to do this. Now the sort by custom_name available from ransack.

    ransacker :custom_name do |_|
      Arel::Nodes::SqlLiteral.new('"core_people"."phone"')
    end