How to search array through ransack gem?
Asked Answered
U

2

4

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?

Ulster answered 16/7, 2016 at 10:14 Comment(0)
D
16

I met the same problem as you do. I'm using Rails 5, and I need to search an array of roles in User table

It seems that you have already add postgres_ext gem in your gemfile, but it has some problems if you are using it in Rails 5 application.

So it is a choice for you to add a contain query in Arel Node by yourself instead of using postgres_ext gem

And if you are using other version of Rails, I think it works well too.

I have an User model, and an array attribute roles. What I want to do is to use ransack to search roles. It is the same condition like yours.

ransack can't search array. But PostgresSQL can search array like this:

User.where("roles @> ?", '{admin}').to_sql)

it produce the sql query like this:

SELECT "users".* FROM "users" WHERE "users"."deleted_at" IS NULL AND (roles @> '{admin}')

So what I want to do is to add a similar contains query in Arel Nodes

You can do it this way:

# app/config/initializers/arel.rb

require 'arel/nodes/binary'
require 'arel/predications'
require 'arel/visitors/postgresql'

module Arel
  class Nodes::ContainsArray < Arel::Nodes::Binary
    def operator
      :"@>"
    end
  end

  class Visitors::PostgreSQL
    private

    def visit_Arel_Nodes_ContainsArray(o, collector)
      infix_value o, collector, ' @> '
    end
  end

  module Predications
    def contains(other)
      Nodes::ContainsArray.new self, Nodes.build_quoted(other, self)
    end
  end
end

Because you can custom ransack predicate, so add contains Ransack predicate like this:

# app/config/initializers/ransack.rb

Ransack.configure do |config|
  config.add_predicate 'contains',
    arel_predicate: 'contains',
    formatter: proc { |v| "{#{v}}" },
    validator: proc { |v| v.present? },
    type: :string
end

Done!

Now, you can search array:

User.ransack(roles_contains: 'admin')

The SQL query will be like this:

SELECT \"users\".* FROM \"users\" WHERE \"users\".\"deleted_at\" IS NULL AND (\"users\".\"roles\" @> '{[\"admin\"]}')

Yeah!

Demirep answered 17/8, 2017 at 6:28 Comment(2)
I advise you to change config.add_predicate 'contains' to config.add_predicate 'contains_array', otherwise you're going to break the other contains functionality for others types of columns. Because I followed what you did here and noticed that side effect.Armoured
This would make a great Pull Request to the Ransack gem. github.com/activerecord-hackery/ransack/issues/897Viole
M
2

I achieved this using ransackable scopes.

Where Author has a column "affiliation" consisting of an array of strings.

scope :affiliation_includes, ->(str) {where("array_to_string(affiliation,',') ILIKE ?", "%#{str}%")}
def self.ransackable_scopes(auth_object=nil) 
    [:affiliation_includes]
  end

then the following works

Author.ransack(affiliation_includes:'dresden')

The advantage of this over use of postgres '@>' is that it can match substrings within the array, because of the %'s in the where command (get rid of %'s for whole string match). It is also case-insensitive (use LIKE instead of ILIKE for case-sensitive.

Mcguinness answered 28/5, 2021 at 1:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.