This is a two-part question.
-
I need to use “contains” in my DB query, though I either need it to NOT be case sensitive OR I need to be able to set the DB value to all lower case in the query.
-
How do I query a “does not contain”?
Thanks.
This is a two-part question.
I need to use “contains” in my DB query, though I either need it to NOT be case sensitive OR I need to be able to set the DB value to all lower case in the query.
How do I query a “does not contain”?
Thanks.
contains
operators and other string matching queries are always case insensitivedoes not contain
operator available, but you may find what you are looking for using the not in
operator. Another option could be defining a helper field
on the model
you are querying so you can directly use the !=
operator.You can find more information on all of the operators available in the Querying DB Objects section of the documentation.
My situation is that I am checking CSV uploaded values against the DB values and sometimes users do not capitalize each word, though in the DB each word is capitalized… for example:
DB value = Ponderosa Pine
CSV value = ponderosa pine
These are the same tree species, but since there is no way to ignore case sensitivity I end up with duplicates in the DB and thus duplicates in dropdown lists.
This is just one case I have, though I am sure many more instances where some form of “does not contain” would be useful.
Any ideas on how to resolve these two issues?
There is not a way to make the DB value all lowercase in the query. Alternatively you can create a helper field that is all lowercase without extra characters, spaces, etc. to use as a lookup.
In your example, you could have a lookup_helper
field that is defined as ponderosa_pine
while keeping the name
defined as Ponderosa Pine
. When completing the lookup from the CSV, you can then complete a search like the example below:
imported_value = imported_value.replace(/\s+/g, '_').toLowerCase();
var db_lookup = DB.tree_species.first('lookup_helper = ?', imported_value);
For the tree diameter descriptions, you could find the “greater than 32 inches” object using the following:
var db_lookup = DB.tree_diameter_descriptions.first('description contains ? and description not in ?', '32', ['greater than 24 inches and less than or equal to 32 inches']);