Better way to do this query?
I have a simple search action for an autocomplete, and I want to have it skip the filter if the search term is
""
in order to just return the top sorted options. I have a pretty simple solution, but was wondering if there's a cleaner/more idiomatic way of doing it:
If not, no problem, this works fine. 🙂11 Replies
🤔 Can
search_string
ever be ""
? IIRC we trim strings by defaultSeems to work as expected. When I empty the search field, I get the top 10, when I enter any character it starts filtering. Trim can produce an empty string, right? That's what I'm matching on.
Sorry, its actually
allow_empty?
which defaults to false
and should be doing "" -> nil
if its not then something weird is going on TBHOh, interesting... When I inspect the search term, it is
nil
. :thinkinghard:
Is it because of the interpolation?oh, lol
search_string != ""
is false when search_string
is NULL
because ash expressions are SQL-ish
Yeah, that makes sense.
Which is all good, that's the behavior I'm looking for.
well, technically it evalutest to
NULL
but yeah it causes the else
branch to runI'm just wondering if there's a better abstraction for this sort of "don't filter empty search strings" idea. I'll be writing a lot of these kinds of actions. And some of them will have much more complex filtering.
Hm…maybe a custom helper macro like
filter_if(predicate, …)