我正在使用ransack gem
在rails應用程序中進行搜索。我需要在User
表中搜索email_ids
的數組。如何通過ransack gem搜索數組?
參考在洗劫this問題,我遵循的步驟,並已將此添加到初始化文件夾ransack.rb
Ransack.configure do |config|
{
contained_within_array: :contained_within,
contained_within_or_equals_array: :contained_within_or_equals,
contains_array: :contains,
contains_or_equals_array: :contains_or_equals,
overlap_array: :overlap
}.each do |rp, ap|
config.add_predicate rp, arel_predicate: ap, wants_array: true
end
end
在軌控制檯,如果我這樣做:
a = User.search(email_contains_array: ['[email protected]'])
它產生sql像這樣:
"SELECT \"users\".* FROM \"users\" WHERE \"users\".\"deleted_at\" IS NULL AND (\"users\".\"email\" >> '---\n- [email protected]\n')"
並給出e RROR這樣的:
User Load (1.8ms) SELECT "users".* FROM "users" WHERE "users"."deleted_at" IS NULL AND ("users"."email" >> '---
- [email protected]
')
ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: operator does not exist: character varying >> unknown
LINE 1: ...RE "users"."deleted_at" IS NULL AND ("users"."email" >> '---
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "users".* FROM "users" WHERE "users"."deleted_at" IS NULL AND ("users"."email" >> '---
- [email protected]
')
預期的那樣,這個查詢:
SELECT "users".* FROM "users" WHERE ("users"."roles" @> '{"3","4"}')
什麼是錯的我在幹嘛?