一起搜查搜索 ID 和字符串
Ransack Search Id and Strings Together
我正在尝试使用 Ransack 进行搜索,但是当我尝试像
一样搜索 id 和字符串时遇到问题
:id_or_title_or_user_username_cont
它产生错误
ActionView::Template::Error (PG::UndefinedFunction: ERROR: operator does not exist: integer ~~* integer
我也试过这个
:id_eq_or_title_or_user_username_cont
它产生以下错误
undefined method `id_eq_or_title_or_user_username_cont' for Ransack::Search
使用 ransack 而非自定义谓词或使用自定义谓词一起搜索 id 和字符串的合适方法是什么?
ransacker :id_to_s do
Arel.sql("regexp_replace(to_char(\"#{table_name}\".\"id\", '9999999'), ' ', '', 'g')")
end
以下postFinding records by ID with Ransack提供了答案:
# Cast the ID column to a string in PostgreSQL
# when searching with Ransack.
# Allows searches with _cont and _eq predicates.
# From https://github.com/ernie/ransack/issues/224
ransacker :id do
Arel.sql("to_char(\"#{table_name}\".\"id\", '99999999')")
end
提供的答案最初来自这个 github 问题:https://github.com/activerecord-hackery/ransack/issues/224
我需要比其他答案更详细的信息,所以就在这里。
对整数列的 _cont
搜索不起作用,因为 LIKE 查询对此类列不起作用。您需要将列从整数转换为字符串才能使查询正常工作。在您的模型中,您可以通过 ransacker
调用来做到这一点:
# app/models/your_model.rb
class YourModel
# For Postgres
ransacker :id do
Arel.sql("to_char(\"#{table_name}\".\"id\", '99999999')")
end
# For MySQL
ransacker :id do
Arel.sql("CONVERT(`#{table_name}`.`id`, CHAR(8))")
end
然后您可以使用像 id_cont
这样的搜索谓词。
小更新,因为上面的答案是正确的,但我发现它们有导致正常结果(例如完整列表,无搜索)按 ID 的字符串值排序的副作用,所以 - 1,10,11 ,12...等...2,20,21...3,30 等而不是 1,2,3,4...
所以结合以上两种思路,可以按ID搜索,但仍然按Int ID排序
在模型中为搜查创建一个字符串化版本的 ID
ransacker :id_as_str do
Arel.sql("to_char(\"#{self.table_name}\".\"id\", '99999999')")
end
并在表格中
<%= f.search_field :id_as_str_or_some_other_stuff_cont, placeholder: "Search..." %>
我正在尝试使用 Ransack 进行搜索,但是当我尝试像
一样搜索 id 和字符串时遇到问题:id_or_title_or_user_username_cont
它产生错误
ActionView::Template::Error (PG::UndefinedFunction: ERROR: operator does not exist: integer ~~* integer
我也试过这个
:id_eq_or_title_or_user_username_cont
它产生以下错误
undefined method `id_eq_or_title_or_user_username_cont' for Ransack::Search
使用 ransack 而非自定义谓词或使用自定义谓词一起搜索 id 和字符串的合适方法是什么?
ransacker :id_to_s do
Arel.sql("regexp_replace(to_char(\"#{table_name}\".\"id\", '9999999'), ' ', '', 'g')")
end
以下postFinding records by ID with Ransack提供了答案:
# Cast the ID column to a string in PostgreSQL
# when searching with Ransack.
# Allows searches with _cont and _eq predicates.
# From https://github.com/ernie/ransack/issues/224
ransacker :id do
Arel.sql("to_char(\"#{table_name}\".\"id\", '99999999')")
end
提供的答案最初来自这个 github 问题:https://github.com/activerecord-hackery/ransack/issues/224
我需要比其他答案更详细的信息,所以就在这里。
对整数列的 _cont
搜索不起作用,因为 LIKE 查询对此类列不起作用。您需要将列从整数转换为字符串才能使查询正常工作。在您的模型中,您可以通过 ransacker
调用来做到这一点:
# app/models/your_model.rb
class YourModel
# For Postgres
ransacker :id do
Arel.sql("to_char(\"#{table_name}\".\"id\", '99999999')")
end
# For MySQL
ransacker :id do
Arel.sql("CONVERT(`#{table_name}`.`id`, CHAR(8))")
end
然后您可以使用像 id_cont
这样的搜索谓词。
小更新,因为上面的答案是正确的,但我发现它们有导致正常结果(例如完整列表,无搜索)按 ID 的字符串值排序的副作用,所以 - 1,10,11 ,12...等...2,20,21...3,30 等而不是 1,2,3,4...
所以结合以上两种思路,可以按ID搜索,但仍然按Int ID排序
在模型中为搜查创建一个字符串化版本的 ID
ransacker :id_as_str do
Arel.sql("to_char(\"#{self.table_name}\".\"id\", '99999999')")
end
并在表格中
<%= f.search_field :id_as_str_or_some_other_stuff_cont, placeholder: "Search..." %>