I'm using ransack gem
for searching in rails application. I need to search an array of email_ids
in User
table.
Referring to this issue at ransacking, I followed the steps and added this to the initializers folder 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
In the rails console, if i do like this:
a = User.search(email_contains_array: ['[email protected]'])
it produces the sql like this:
"SELECT \"users\".* FROM \"users\" WHERE \"users\".\"deleted_at\" IS NULL AND (\"users\".\"email\" >> '---\n- [email protected]\n')"
and gives error like this:
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]
')
Expected is this query:
SELECT "users".* FROM "users" WHERE ("users"."roles" @> '{"3","4"}')
What is wrong am I doing?
config.add_predicate 'contains'
toconfig.add_predicate 'contains_array'
, otherwise you're going to break the othercontains
functionality for others types of columns. Because I followed what you did here and noticed that side effect. – Armoured