Adn*_*nan 6 activerecord ruby-on-rails rails-activerecord
我是 Ruby on rails 的新手,我想根据条件获取记录,并以字符串格式传递条件。此外,我将在多个 OR 和 AND 条件下传递查询。但是,现在,我遇到了如何在 Rails 中以字符串格式传递查询的问题,我附上了屏幕截图 
@data= CustomAttribute.includes(:custom_attribute_values).where(id: 18, company_id: current_user.company_id).first
Run Code Online (Sandbox Code Playgroud)
上面的行成功执行并给出了输出
<CustomAttribute id: 18, data_type: "string", label: "Marital status", code: "marital_status", entity_type: "member", company_id: 1, created_at: "2021-03-10 10:16:15", updated_at: "2021-03-10 10:16:27", is_active: true, is_default: false, rank: nil, is_identifier: false>
Run Code Online (Sandbox Code Playgroud)
但是当我执行以下行时,它给了我错误
@data.custom_attribute_values.where("\""+"value_string"+"\""+"="+"\""+'Single'+"\"").size
Run Code Online (Sandbox Code Playgroud)
错误:“单个”列不存在
Single 是我想计算的值
这是我的动态查询创建代码
logical_operator = 'OR'
@custom_attribute = CustomAttribute.includes(:custom_attribute_values).where(id: custom_attribute_ids, company_id: current_user.company_id)
query=""
@custom_attribute.each_with_index do |attribute_object, index|
filter_object= filter_params[:filters].find {|x| x['custom_attribute_id']==attribute_object['id']}
if filter_object.present?
query += "("+ '"' +'value_'+attribute_object.data_type + '"' + ' ' + filter_object['operator'] + ' ' + "'" + filter_object['value'].to_s + "'"+ ")"
end
if index != @custom_attribute.length-1
query+=' '+logical_operator+' '
end
if index == @custom_attribute.length-1
query="'" + " ( " + query + " ) " + "'"
end
end
byebug
puts(@custom_attribute.first.custom_attribute_values.where(query).size)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
63 次 |
| 最近记录: |