我正在尝试进行查询以搜索hstore列属性中的值。我按用户输入按属性过滤问题。可以搜索电子邮件是X的问题,或者电子邮件是X而发件人是“某人”的问题。很快我需要更改为使用LIKE搜索类似的结果。因此,如果您也知道如何使用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
我需要知道如何使用多个键/值对查询属性,“OR”或“AND”无关紧要。
我希望得到一个或多个结果,其中包含我正在寻找的那些值。
答案 0 :(得分:1)
我最终这样做了。使用方法的array选项在哪里。同时在评论中使用@anusha的建议。 IDK为什么要downvote,我找不到任何关于如何做这样简单的事情。我怀疑格式化我的查询,主要是使用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