Search code examples
ruby-on-railsransack

Ransacker : Unsupported argument type: String. Construct an Arel node instead


I am trying to contact two fields and then search using ransack. I have a User model and two fields: first_name and last_name and I want to search users using full name. Here is what I am using :

ransacker :full_name do |parent|
  Arel::Nodes::InfixOperation.new('||',
    Arel::Nodes::InfixOperation.new('||',
      parent.table[:first_name], ' '
    ),
    parent.table[:last_name]
  )
end

but I am getting this error: Unsupported argument type: String. Construct an Arel node instead. after passing the fullname into search field.


Solution

  • I just replace my code with following:

    ransacker :full_name, formatter: proc { |v| v.mb_chars.downcase.to_s } do |parent|
      Arel::Nodes::NamedFunction.new('LOWER',
       [Arel::Nodes::NamedFunction.new('concat_ws',
        [Arel::Nodes.build_quoted(' '), parent.table[:first_name], 
      parent.table[:last_name]])])
    end
    

    This solved the issue.