简体   繁体   English

Neo4j gem-根据参数值查询

[英]Neo4j gem - Query dependant on parameter values

This might not be as much neo4j as it is rails. 它可能不像rails那么多neo4j。 But maybe I'm wrong 但是也许我错了

I'm looking for suggestions on handling a situation when parameters are specific values. 我正在寻找有关在参数为特定值时处理情况的建议。 For example. 例如。 In my query 在我的查询中

@friends_events = current_user.friends.events(:e, :rel).where("rel.admin = {admin_p} AND e.size_max < {size_p}", uuid: @event_id, primary_category: params[:primary_category] ).params(admin_p: true, size_p: params[:group_max].to_i)

The event has a size_max property which can be an integer or it can be any . 该事件具有size_max属性,该属性可以是整数,也可以是any Right now I have any as a blank value. 现在我有any空白值。 Basically if they choose any, I need to ignore that parameter all together in the query (or handle it in a similar fashion). 基本上,如果他们选择了任何一个,我都需要在查询中一起忽略该参数(或以类似的方式处理)。 A way to cheat it is to handle the situation outside and if any is selected and the value is blank, I manually set it to a really high number which won't be hit. 作弊的一种方法是处理外部情况,如果选择了任何情况且该值是空白,则我将其手动设置为一个不会被击中的很高的数字。

Not sure how to do this either inside or outside the query without an odd hacky way right now. 不知道如何在查询内部或外部执行此操作,而现在没有奇怪的方法。 Suggestions? 建议?

Update 更新

I have my query as you suggested. 我有你的建议。 and the methods to deal with the 'validation' 以及处理“验证”的方法

@friends_events = current_user.friends.events(:e, :rel).where("rel.admin = {admin_p} #{size_string}", uuid: @event_id, primary_category: params[:primary_category] ).params(admin_p: true, size_p: size_param)

And I changed the size_param to blank which it doesn't like. 然后我将size_param更改为不喜欢的空白。 I wanted to be able to handle both cases. 我希望能够处理这两种情况。 eg when you first hit the page params is empty, and when you submit, it's blank. 例如,当您第一次点击页面时,参数为空,而当您提交时,则为空白。 Nil will work with scenario 1 and not scenario 2. Do I need a || Nil适用于方案1,而不适用于方案2。我需要||吗? case here? 案例在这里?

        def size_string
          'AND e.size_max < {size_p}' if params[:group_max]
        end

        def size_param
            #has not taken in blank scenario
          params[:group_max].blank? ? false : params[:group_max].to_i
        end

and in my view I have 在我看来,我有

<% if !@friends_events.blank? %>

My error is 我的错误是

Don't know how to compare that. Left: 0 (Long); Right: false (Boolean) Don't know how to compare that. Left: 0 (Long); Right: false (Boolean) from the line above in my view. Don't know how to compare that. Left: 0 (Long); Right: false (Boolean)在我看来,以上一行为Don't know how to compare that. Left: 0 (Long); Right: false (Boolean) Changing .blank? 更改.blank? to .nil? .nil? allows the filter to go through (though incorrectly) 允许过滤器通过(尽管不正确)

The ways of handling it that you identified seem like the best options. 您确定的处理方式似乎是最佳选择。 It may be a little more work to evaluate it before hitting the database but you'll get a performance boost by omitting that property entirely if the user wants any . 这可能是一点点更多的工作进入数据库之前评估它,但你会省略完全如果用户希望该属性获得性能提升any I have been told that filtering using > or <, does not use indexes, so it may be slow if you have enough records. 有人告诉我,使用>或<进行过滤不会使用索引,因此,如果您有足够的记录,可能会很慢。 Start with something like this: 首先是这样的:

def where_this_happens
  @friends_events = current_user.friends.events(:e, :rel)
    .where("rel.admin = {admin_p} #{size_string}", uuid: @event_id, primary_category: params[:primary_category] )
    .params(admin_p: true, size_p: size_param)
end

def size_string
  'AND e.size_max < {size_p}' unless params[:group_max].blank?
end

def size_param
  params[:group_max].nil? ? false : params[:group_max].to_i
end

There's no harm in setting the size_p param and not using it but I'm pretty sure it will bark at you if you feed it nil . 有没有在设置无妨size_p PARAM并没有使用它,但我敢肯定它会在你吠叫,如果你给它nil

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

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