简体   繁体   English

Ransack与数组值完全匹配

[英]Ransack exact match with array values

I have to perform a search on a database using ransack. 我必须使用ransack在数据库上执行搜索。 A few columns in the database have data stored in serialized arrays. 数据库中的一些列将数据存储在序列化数组中。 I want to match the exact data stored in the arrays with data sent by user to perform search (users' data are also arrays). 我想将存储在数组中的确切数据与用户发送的数据进行匹配以执行搜索(用户的数据也是数组)。 For example, in database, one column has data as ( c1 , c2 are test cases): 例如,在数据库中,一列的数据为( c1c2为测试用例):

c1.column_data = [1, 2, 3, 4, 5]
c2.column_data = []

User searches for data ( t1 , t2 , t3 are test cases): 用户搜索数据( t1t2t3是测试用例):

t1.user_data = [1]
t2.user_data = [1, 3]
t3.user_data = [1, 2, 3, 4, 5]
t4.user_data = []
  • For case c1 with t1 , t2 , t4 , it should return no match found . 对于具有t1t2t4情况c1 ,它应该返回no match found
  • With t3 , it should result with match found . 使用t3 ,应该match found结果。
  • For case c2 with t1 , t2 , t3 , it should return no match found . 对于具有t1t2t3情况c2 ,它应该返回no match found
  • With t4 , it return `match found. 使用t4 ,它返回`匹配找到。

I found postgres_ext gem , but I cannot make it work. 我找到了postgres_ext gem ,但我无法使它工作。 Can anyone suggest how I can do this or suggest any alternative method for search like this? 任何人都可以建议我如何做到这一点或建议任何替代搜索方法这样的? Any alternative solution is also welcome. 任何替代解决方案也欢迎。

I am answering my own question. 我在回答我自己的问题。

It may vary to the requirement. 它可能会有所不同。

I had to take the user params and search in the database if any exact data matches then put the user in same group else make a new group with the searched data and at the end save each searched data for respective user also. 我必须使用用户参数并在数据库中搜索是否有任何确切数据匹配,然后将用户放在同一组中,否则用搜索到的数据创建一个新组,最后也为各个用户保存每个搜索数据。

As all the fields were multi select dropdowns checkboxes etc all params was coming as an array of strings. 由于所有字段都是多选下拉列表复选框等所有参数都是作为字符串数组出现的。

My workaround for the problem: 1. sorted the array params and joined it with comma and made string. 我对该问题的解决方法:1。对数组params进行排序,并使用逗号和字符串连接它。 ex: 例如:

    a = [1,2,3,4,5,6]
    a.sort.join(',')
    "1,2,3,4,5,6"

I stored this string in the database column which is in text. 我将此字符串存储在文本中的数据库列中。 No when user searches for a group I take its params and convert it again in comma separated string and using where clause query to database is done. 否,当用户搜索组时,我会使用其参数并以逗号分隔的字符串再次转换它并使用where子句查询到数据库。

In UI I convert this string to array again and show it to user. 在UI中,我再次将此字符串转换为数组并将其显示给用户。

I dont know its a correct way of doing it or not but it is working for me. 我不知道它是否正确的做法,但它对我有用。

still any good solutions are welcome. 仍然欢迎任何好的解决方案。

ok, with rails 5.1 simple Ransack search will works fine. 好吧,使用rails 5.1简单的Ransack搜索将正常工作。 For example assuming one of your columns with serialised array name is 'column_data' 例如,假设您的一个具有序列化数组名称的列是'column_data'

t.text :column_data, array: true, default: []

you can search the array columns by following this syntax: 您可以按照以下语法搜索数组列:

‘{ val1, val2, … }’

for example in console 例如在控制台中

some_search = Model.search(column_data_eq: '{1,3}')
some_search.result

should give you the following results 应该给你以下结果

SELECT  "model".* FROM "mdole" WHERE "model"."column_data" = '{1,3}' LIMIT $1  [["LIMIT", 11]]

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

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