Ahm*_*mza 3 ruby postgresql activerecord ruby-on-rails ransack
我正在ransack gem用于在rails应用程序中搜索.我需要搜索的数组email_ids中的User表.
在洗劫时提到这个问题,我按照步骤将其添加到初始化文件夹中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
Run Code Online (Sandbox Code Playgroud)
在rails控制台中,如果我喜欢这样:
a = User.search(email_contains_array: ['priti@gmail.com'])
Run Code Online (Sandbox Code Playgroud)
它产生这样的sql:
"SELECT \"users\".* FROM \"users\" WHERE \"users\".\"deleted_at\" IS NULL AND (\"users\".\"email\" >> '---\n- priti@gmail.com\n')"
Run Code Online (Sandbox Code Playgroud)
并给出如下错误:
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
')
Run Code Online (Sandbox Code Playgroud)
预期是这个查询:
SELECT "users".* FROM "users" WHERE ("users"."roles" @> '{"3","4"}')
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
小智 9
我遇到了和你一样的问题.我使用Rails 5,我需要搜索的数组roles中的User表
您似乎已经添加postgres_ext gem了gemfile,但如果您在Rails 5应用程序中使用它,则会出现一些问题.
因此,您可以contain自行选择在Arel Node中添加查询,而不是使用postgres_ext gem
如果您使用的是其他版本的Rails,我认为它的效果也很好.
我有一个User模型和一个数组属性roles.我想要做的是ransack用来搜索roles.这和你的情况一样.
ransack无法搜索数组.但是PostgresSQL可以像这样搜索数组:
User.where("roles @> ?", '{admin}').to_sql)
它产生这样的SQL查询:
SELECT "users".* FROM "users" WHERE "users"."deleted_at" IS NULL AND (roles @> '{admin}')
Run Code Online (Sandbox Code Playgroud)
所以我想要做的是添加一个类似的包含查询 Arel Nodes
你能做到这样:
# 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
Run Code Online (Sandbox Code Playgroud)
因为你可以自定义洗劫谓语,所以加contains搜查谓语像这样:
# 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
Run Code Online (Sandbox Code Playgroud)
完成!
现在,您可以搜索数组:
User.ransack(roles_contains: 'admin')
Run Code Online (Sandbox Code Playgroud)
SQL查询将如下所示:
SELECT \"users\".* FROM \"users\" WHERE \"users\".\"deleted_at\" IS NULL AND (\"users\".\"roles\" @> '{[\"admin\"]}')
Run Code Online (Sandbox Code Playgroud)
是啊!