Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Searching for full name #490

Closed
JanStevens opened this issue Jan 7, 2015 · 6 comments
Closed

Searching for full name #490

JanStevens opened this issue Jan 7, 2015 · 6 comments

Comments

@JanStevens
Copy link

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!

@jonatack
Copy link
Contributor

jonatack commented Jan 7, 2015

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

@JanStevens
Copy link
Author

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,

@JanStevens
Copy link
Author

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(' ').

@jonatack
Copy link
Contributor

jonatack commented Jan 7, 2015

Thanks for the info! 💚

Will update the wiki example.

@jonatack
Copy link
Contributor

jonatack commented Jan 9, 2015

Wiki updated.

@dfurber
Copy link

dfurber commented Feb 20, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants