简体   繁体   English

如何通过ransack gem搜索数组?

[英]How to search array through ransack gem?

I'm using ransack gem for searching in rails application. 我正在使用ransack gem在rails应用程序中进行搜索。 I need to search an array of email_ids in User table. 我需要在User表中搜索email_ids数组。

Referring to this issue at ransacking, I followed the steps and added this to the initializers folder ransack.rb 在洗劫时提到这个问题,我按照步骤将其添加到初始化文件夹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: 在rails控制台中,如果我喜欢这样:

a = User.search(email_contains_array: ['priti@gmail.com'])

it produces the sql like this: 它产生这样的sql:

"SELECT \"users\".* FROM \"users\" WHERE \"users\".\"deleted_at\" IS NULL AND (\"users\".\"email\" >> '---\n- priti@gmail.com\n')"

and gives error like this: 并给出如下错误:

  User Load (1.8ms)  SELECT "users".* FROM "users" WHERE "users"."deleted_at" IS NULL AND ("users"."email" >> '---
- priti@gmail.com
')
 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" >> '---
 - priti@gmail.com
')

Expected is this query: 预期是这个查询:

SELECT "users".* FROM "users"  WHERE ("users"."roles" @> '{"3","4"}')

What is wrong am I doing? 我做错了什么?

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 我正在使用Rails 5,我需要在User表中搜索一组roles

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. 您似乎已经在gemfile中添加了postgres_ext gem ,但如果您在Rails 5应用程序中使用它,则会出现一些问题。

So it is a choice for you to add a contain query in Arel Node by yourself instead of using postgres_ext gem 因此,您可以自行选择在Arel Node中添加contain查询,而不是使用postgres_ext gem

And if you are using other version of Rails, I think it works well too. 如果您使用的是其他版本的Rails,我认为它的效果也很好。

I have an User model, and an array attribute roles . 我有一个User模型和一个数组属性roles What I want to do is to use ransack to search roles . 我想要做的是使用ransack来搜索roles It is the same condition like yours. 这和你的情况一样。

ransack can't search array. ransack无法搜索数组。 But PostgresSQL can search array like this: 但是PostgresSQL可以像这样搜索数组:

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

it produce the sql query like this: 它产生这样的SQL查询:

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 所以我想要做的是在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 : 因为你可以自定义ransack谓词,所以add contains Ransack谓词,如下所示

# 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: SQL查询将如下所示:

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

Yeah! 是啊!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM