Reputation: 945
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)
Upvotes: 1
Views: 70
Reputation: 5482
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
.
Upvotes: 2