1

This might not be as much neo4j as it is rails. 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. Right now I have any as a blank value. 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. I wanted to be able to handle both cases. e.g. 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 || 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) from the line above in my view. Changing .blank? to .nil? allows the filter to go through (though incorrectly)

Clam
  • 935
  • 1
  • 12
  • 24

1 Answers1

2

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. 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.

subvertallchris
  • 5,282
  • 2
  • 25
  • 43
  • got a bit confused reading the logic there. should `size_param(size_string)` be actually `size_param(params[:group_max])` – Clam Dec 03 '14 at 00:30
  • No, it should be what I just edited it to. That was left over from an earlier version. You don't need any method attributes since it's just looking at params. – subvertallchris Dec 03 '14 at 00:36
  • okay, i made an adjustment to make it `params[:group_max].blank?` but i'm getting complaints on the end of my rendering. when i render, i had a check like `!@friends_events.first.nil?`. this isn't able to make a comparison because it says it's comparing a long to a boolean. was not aware that the queryproxy object retrieved becomes a long...? I've had other variations of that nil check/blank check, but similar issues – Clam Dec 03 '14 at 01:42
  • 1
    Whaaa? Don't do `!@friends_events.first.nil?`, do `!@friends_events.blank?`, it'll run serverside and be faster. Edit your question and share the code you're working with if still having trouble. – subvertallchris Dec 03 '14 at 02:42
  • Add in your final `size_param` and `size_string` methods, too. – subvertallchris Dec 03 '14 at 06:54
  • Updated again with both methods – Clam Dec 03 '14 at 07:32
  • Is that error coming up when they select "any" or a number? It's telling you that you're making a comparison with `false`, so `size_string` is outputting the string and `size_param` is giving false. The first should only give you the string if they select a specific number and when that happens, what the second gives doesn't matter since it's not part of the match anyway. – subvertallchris Dec 03 '14 at 17:21
  • Play with the cypher in the Neo4j browser. – subvertallchris Dec 03 '14 at 17:22
  • oddly it seems that i was always returning the statement `AND e.size_max < {size_p}`so i made a change to this in the `size_strong` method `AND e.size_max < {size_p}' if params[:group_max].is_a? Integer` – Clam Dec 03 '14 at 20:24