Ransack: Searching for full name

Created on 7 Jan 2015  路  6Comments  路  Source: activerecord-hackery/ransack

hello,

After upgrading to Arel 6 the following full name search method does not work anymore

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

The following error is triggered:

RuntimeError - unsupported: String:
  arel (6.0.0) lib/arel/visitors/to_sql.rb:729:in `unsupported'
  arel (6.0.0) lib/arel/visitors/reduce.rb:13:in `visit'
  arel (6.0.0) lib/arel/visitors/to_sql.rb:750:in `visit_Arel_Nodes_InfixOperation'
  arel (6.0.0) lib/arel/visitors/reduce.rb:13:in `visit'
  arel (6.0.0) lib/arel/visitors/to_sql.rb:748:in `visit_Arel_Nodes_InfixOperation'
  arel (6.0.0) lib/arel/visitors/reduce.rb:13:in `visit'
  arel (6.0.0) lib/arel/visitors/to_sql.rb:796:in `infix_value'
  arel (6.0.0) lib/arel/visitors/postgresql.rb:7:in `visit_Arel_Nodes_Matches'
  arel (6.0.0) lib/arel/visitors/reduce.rb:13:in `visit'
  arel (6.0.0) lib/arel/visitors/to_sql.rb:648:in `visit_Arel_Nodes_Or'
  arel (6.0.0) lib/arel/visitors/reduce.rb:13:in `visit'
  arel (6.0.0) lib/arel/visitors/to_sql.rb:437:in `visit_Arel_Nodes_Grouping'
  arel (6.0.0) lib/arel/visitors/reduce.rb:13:in `visit'
  arel (6.0.0) lib/arel/visitors/to_sql.rb:434:in `visit_Arel_Nodes_Grouping'
  arel (6.0.0) lib/arel/visitors/reduce.rb:13:in `visit'
  arel (6.0.0) lib/arel/visitors/to_sql.rb:788:in `block in inject_join'
  arel (6.0.0) lib/arel/visitors/to_sql.rb:786:in `inject_join'
  arel (6.0.0) lib/arel/visitors/to_sql.rb:644:in `visit_Arel_Nodes_And'
  arel (6.0.0) lib/arel/visitors/reduce.rb:13:in `visit'
  arel (6.0.0) lib/arel/visitors/to_sql.rb:255:in `block in visit_Arel_Nodes_SelectCore'
  arel (6.0.0) lib/arel/visitors/to_sql.rb:254:in `visit_Arel_Nodes_SelectCore'
  arel (6.0.0) lib/arel/visitors/to_sql.rb:210:in `block in visit_Arel_Nodes_SelectStatement'
  arel (6.0.0) lib/arel/visitors/to_sql.rb:209:in `visit_Arel_Nodes_SelectStatement'
  arel (6.0.0) lib/arel/visitors/reduce.rb:13:in `visit'
  arel (6.0.0) lib/arel/visitors/reduce.rb:7:in `accept'
  activerecord (4.2.0) lib/active_record/connection_adapters/abstract/database_statements.rb:12:in `to_sql'
  activerecord (4.2.0) lib/active_record/connection_adapters/abstract/query_cache.rb:67:in `select_all'
  activerecord (4.2.0) lib/active_record/relation/calculations.rb:264:in `execute_simple_calculation'
  activerecord (4.2.0) lib/active_record/relation/calculations.rb:221:in `perform_calculation'
  activerecord (4.2.0) lib/active_record/relation/calculations.rb:127:in `calculate'
  activerecord (4.2.0) lib/active_record/relation/calculations.rb:42:in `count'

Arel is a bit out of my league, I tried some stuff but since there is virtually no documentation I'm kinda in the dark. Thanks!

Most helpful comment

And solved my own question, apparently I was pretty close, Arel now requires you to quote the string so

ransacker :full_name do |parent|
  Arel::Nodes::InfixOperation.new('||',
    Arel::Nodes::InfixOperation.new('||',
      parent.table[:firstname], Arel::Nodes.build_quoted(' ')
    ),
    parent.table[:lastname]
  )
end

Is the new way to do a full name search -- notice the Arel::Nodes.build_quoted(' ').

All 6 comments

Thanks.
Versions please? Perhaps a failing spec? We need to be able to reproduce the issue in your environment (see the Contributing Guide).

Lets see:
arel 6.0.0
ransack: master
rails: 4-2-stable (d4fe454e42e533b8ad27a46596798fc13ac6d4f3)

I'm pretty sure this is more of an Api Arel change then something that has to do with ransack itself (maybe I better post my question in Arel issue tracker).

Anyway the Arel::Nodes::InfixOperation doesnt like the last one space parameter (I tried changing it to nil and then I got the same error but with RuntimeError - unsupported: NilClass:

Regards,

And solved my own question, apparently I was pretty close, Arel now requires you to quote the string so

ransacker :full_name do |parent|
  Arel::Nodes::InfixOperation.new('||',
    Arel::Nodes::InfixOperation.new('||',
      parent.table[:firstname], Arel::Nodes.build_quoted(' ')
    ),
    parent.table[:lastname]
  )
end

Is the new way to do a full name search -- notice the Arel::Nodes.build_quoted(' ').

Thanks for the info! :green_heart:

Will update the wiki example.

Wiki updated.

I ran into a similar issue on the version that uses the ws_concat (the last one in the example). Here is the working version if you want to update the wiki:

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AnnaErshova picture AnnaErshova  路  3Comments

timoschilling picture timoschilling  路  5Comments

seanfcarroll picture seanfcarroll  路  4Comments

llopez picture llopez  路  4Comments

tagrudev picture tagrudev  路  3Comments