Gem Ransack doesn't return any results when searched with full name

我的未来我决定 提交于 2019-12-04 12:09:36

You have to use a Ransacker. Add this in your User model:

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

You can check the Ransack GitHub wiki to more examples.

Add the following code to your model:

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

... and the following in your view:

<%= f.label :full_name %><br>
<%= f.search_field :full_name_cont %>

The ransacker part was taken from the Ransack wiki.

Tested on Rails 4.2 with PostgreSQL.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!