Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search array through ransack gem?

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?

like image 483
Ahmad hamza Avatar asked Dec 19 '22 15:12

Ahmad hamza


1 Answers

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!

like image 125
Ivyxuan Avatar answered Dec 30 '22 17:12

Ivyxuan