简体   繁体   English

如何在Rails中查询数组列? 在哪里条件?

[英]how to query an array column in rails? with where condition?

i am having the following columns in my table t.string :appr_status , type: Array, default: [] and t.integer :employee_id and my current query is 我在表t.string :appr_status , type: Array, default: []具有以下列t.string :appr_status , type: Array, default: []t.integer :employee_id ,当前查询是

     @reg_req = AttendanceChangeRequest.where("employee_id IN (?)", emp_id_arr).to_a

where emp_id_arr is an array like [1,2,3] . 其中emp_id_arr是类似于[1,2,3]的数组。 and i want to change the above query to this. 我想将以上查询更改为此。

    AttendanceChangeRequest.where('(employee_id IN = ? AND appr_status contain= ?) ', emp_id_arr , appr_array )

where appr_array = ["1_app","2_app", "3_appr","4_appr"] is this. 其中appr_array = [“ 1_app”,“ 2_app”,“ 3_appr”,“ 4_appr”]是这里。 i want to get all the records when any one of the employee id is present in the passing array that is emp_id_arr and appr_status column should contain all values which are present in the passing array to the query (that is in appr_array ) if one value is also not present then i dont want that record. 当一个雇员ID出现在emp_id_arr的传递数组中时,我想获取所有记录,并且appr_status列应包含查询的传递数组(即appr_array )中存在的所有值(如果一个值是)还不存在,那么我不想那个记录。 how to write this query? 如何编写此查询?

u need to like 你需要喜欢

AttendanceChangeRequest.where("'1_app' = ANY (appr_status)")

This will work correctly. 这将正常工作。

Try this: 尝试这个:

AttendanceChangeRequest.where(employee_id: emp_id_arr,appr_status: appr_array)

This is what you are looking? 这是你在找什么?

AttendanceChangeRequest.where("array_to_string(appr_status, '||') ILIKE :name", name: "%your desire search able value here%") AttendanceChangeRequest.where(“ array_to_string(appr_status,'||')ILIKE:name”,名称:“%your want search能力值%%”)

This is what you are looking? 这是你在找什么? what this query ll do. 这个查询会做什么。

  • this ll find the record from table where colum of appr_status having datatype array contain your desire given value. 这将从表中找到记录,其中具有数据类型数组的appr_status列包含您想要的给定值。 For example appr_status col has array [1,2,3,4] user want to fatch record from appr_status col where each array of appr_status contain integer 3 here 3 will be your desire given value you can put it via variable or hardcot 例如,appr_status col具有数组[1,2,3,4],用户希望从appr_status col记录记录,其中appr_status的每个数组都包含整数3,这里3是您想要的给定值,您可以通过变量或硬编码来放置它

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

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