Let's imagine I have a few simple docs stored in an Arango collection like so:
[
{"type":Cat, "quality":Fuzzy}
{"type":Dog, "quality":Barks}
{"type":Rabbit, "quality":Hoppy}
{"type":Pig, "quality":Chubby}
{"type":Red Panda, "quality":Fuzzy}
{"type":Monkey, "quality":Hairy}
]
Now let's say a user initiates a search in my application for all animals that are 'fuzzy', all lower case. Is there a way with AQL to make a comparison that is not case sensitive? So for instance:
FOR a IN animals
FILTER a.type.toLowerCase() == fuzzy
RETURN a
Now I know the above example doesn't work, but it would be nice if there was a way to do this. Thanks!
There is a LOWER string function in AQL which you can try to use in your query like this:
FOR a IN animals
FILTER LOWER(a.quality) == 'fuzzy'
RETURN a
Related
I need to find a record from 2 parameters but I need one of them to be case insensitive. The current case sensitive line is
c = Course.find_by(subject_area: area, cat_number: cat)
But I need subject_area to be case insensitive. How would I achieve that?
It depends on the database, and you may need to pass in db-specific SQL for that (and not use find_by).
Are you using postrges? if so, this would normally work:
Course.where("LOWER(subject_area) = ? AND cat_number = ?", area.downcase, cat)
alternatively you could convert your subject_area to downcase every time you save a new one... then just use:
Course.find_by(subject_area: area.downcase, cat_number: cat)
I could be wrong, but don't currently know of any rails-native way of doing a case insensitive rails find_by
An alternative can be
c = Course.find_by("LOWER(subject_area)= ? AND cat_number = ?", area.downcase, cat)
If you are using postgres, you could do something like:
> c = Course.find_by('subject_area ILIKE ? AND cat_number LIKE ?', area, cat)
(ILIKE is used to do insensitive search)
If you need to have this attribute be case-insensitive then the right way is to ensure it's always one particular in your model
before_save { self.subject_area = subject_area.downcase }
You can migrate your existing data to be downcased as well.
Having done that your find by operation still needs to ensure that the area is downcased in query as suggested.
Course.find_by(subject_area: area.downcase, cat_number: cat)
Option 2:
I knew I was forgetting this. Let's say you dont want to go migration path. Just define a scope.
http://guides.rubyonrails.org/v4.1/active_record_querying.html#scopes
scope :area_nocase, (area) -> { where("LOWER(subject_area) = ?", area.downcase) }
then you can use it like
Course.area_nocase(area) or cat.course.area_nocase(area)
or chain it however you need to.
I am trying to use a wildcard or regular expression to give some leeway with user input in retrieving information from a database in a simple library catalog program, written in Ruby.
The code in question (which currently works if there is an exact match):
puts "Enter the title of the book"
title = gets.chomp
book = $db.execute("SELECT * FROM books WHERE title LIKE ?", title).first
puts %Q{Title:#{book['title']}
Author:#{book['auth_first']} #{book['auth_last']}
Country:#{book['country']}}
I am using SQLite 3. In the SQLite terminal I can enter:
SELECT * FROM books WHERE title LIKE 'Moby%'
or
SELECT * FROM books WHERE title LIKE "Moby%"
and get (assuming there's a proper entry):
Title: Moby-Dick
Author: Herman Melville
Country: USA
I can't figure out any corresponding way of doing this in my Ruby program.
Is it not possible to use the SQL % wildcard character in this context? If so, do I need to use a Ruby regular expression here? What is a good way of handling this?
(Even putting the ? in single quotes ('?') will cause it to no longer work in the program.)
Any help is greatly appreciated.
(Note: I am essentially just trying to modify the sample code from chapter 9 of Beginning Ruby (Peter Cooper).)
The pattern you give to SQL's LIKE is just a string with optional pattern characters. That means that you can build the pattern in Ruby:
$db.execute("SELECT * FROM books WHERE title LIKE ?", "%#{title}%")
or do the string work in SQL:
$db.execute("SELECT * FROM books WHERE title LIKE '%' || ? || '%'", title)
Note that the case sensitivity of LIKE is database dependent but SQLite's is case insensitive so you don't have to worry about that until you try to switch database. Different databases have different ways of dealing with this, some have a case insensitive LIKE, some have a separate ILIKE case insensitive version of LIKE, and some make you normalize the case yourself.
In my rails app I am trying to search the Users model based on certain conditions.
In particular, I have a location field which is a string and I want to search this field based on whether it contains the search string. For example, if I search for users with location 'oxford' I want it to also return users with a variation on that, like 'oxford, england'.
Having searched the web for the answer to this it seems that I should be using the LIKE keyword in the activerecord search, but for me this is only returning exact matches.
Here is a snippet of my code from the search method
conditions_array = []
conditions_array << [ 'lower(location) LIKE ?', options[:location].downcase ] if !options[:location].empty?
conditions = build_search_conditions(conditions_array)
results = User.where(conditions)
Am I doing something wrong? Or is using LIKE not the right approach to achieving my objective?
You need to do like '%oxford%'
% Matches any number of characters, even zero characters
conditions_array << [ 'lower(location) LIKE ?', "%#{options[:location].downcase}%" ] if !options[:location].empty?
I'm making a webapp where I'm using MongoMapper and Sinatra. I wonder how could I implement a search feature against a DB's collection. I though something like SQL's:
SELECT * FROM posts WHERE match(title) against ("String to search");
How could I achieve this with MongoMapper? Thanks!
ok this is from my project and does work:
Post.where(:title => Regexp.new(/^string/i)) # Limit output with: .limit(10)
Maybe it's the Regexp?
You query for documents that match a case sensitive rooted regular expression:
Post.where(:title => /^stringtosearch/).first
MongoDB does not support full text search so this is the best you can do at the moment.
I previously asked a question regarding pulling specific items out of a database if they contained a specific word in their string, someone kindly offered the following which did just the job:
def SomeModel < ActiveRecord::Base
scope :contains_city,
lambda { |city| where("some_models.address LIKE ?","%"+city+"%" ) }
end
However, I have some instances where I would like to do the opposite, i.e. pull out all the items which do not have the specified word in their string. Is there a way to do a NOT LIKE function? I have prevously seen people use '!=' for a NOT EQUALS, but have had no success along these lines for the LIKE function. Is there an equivalent or is it best to iterate through the database putting items in 2 separate databases based on whether they satisfy the LIKE condition?
You could try NOT LIKE in your query; MySQL supports this.
http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html