查询使用Rails Postgres Hstore的多个密钥值



我正在尝试进行查询以搜索我的Hstore列属性中的值。我正在通过属性通过用户输入过滤问题。可以搜索电子邮件为X的问题,或者电子邮件为X且发件人为"某人"的问题。很快,我需要更改以搜索类似的结果。因此,如果您也知道如何使用like,请显示这两个选项。

如果我这样做:

Issue.where("properties @> ('email => pugozufil@yahoo.com') AND properties @> ('email => pugozufil@yahoo.com')")

它返回问题。

如果我这样做:

Issue.where("properties @> ('email => pugozufil@yahoo.com') AND properties @> ('sender => someone')")

在这里我有一个错误,告诉我:

ERROR:  Syntax error near 'd' at position 11

我将"@>"更改为" ->",现在显示此错误:

PG::DatatypeMismatch: ERROR: argument of AND must be type boolean, not type text

我需要知道如何用"或"或","无关紧要。

我希望获得一个或多个结果,其中包括我要寻找的那些值。

我最终这样做。使用该方法的数组选项。还在评论中使用@anusha的建议。idk为什么要辞职,我找不到有关如何做这样简单的事情的任何事情。我怀疑要格式化查询,主要是与Hstore一起使用。因此,我希望将来它能帮助某人现在对我有帮助。

if params[:filter].present?
    filters = params[:filter]
    conditions = ["properties -> "]
    query_values = []
    filter_query = ""
    filters.each do |k, v|
      if filters[k].present?
        filter_query += "'#{k}' LIKE ?"
        filter_query += " OR "
        query_values << "%#{v}%"
      end
    end
    filter_query = filter_query[0...-(" OR ".size)] # remove the last ' OR '
    conditions[0] += filter_query
    conditions = conditions + query_values
    @issues = @issues.where(conditions)
end

最新更新