How to make search box case-insensitive in rails? - ruby-on-rails

I have simple search form in Rails, but when I using the search box, I need to enter the name exactly.
For instance, when I try to search for "more" it doesn't return anything, but when I do with "More", then it returns the records, So it seems like it behaves in a case-sensitive way.
Is it possible to make this case-sensitive way?
Here is my code
def self.search(search)
if search
star = Star.find_by(who: search)
if star
self.where(star_id: star)
else
Post.all
end
else
Post.all
end
end

You could do something like:
star = Star.where("UPPER(who) = ?", search.upcase).take
or
star = Star.where("LOWER(who) = ?", search.downcase).take
Either way, this coerces both your search term as well as the who value in the database before comparing them, which should get you the results that you need

Related

Rails Search Box

So I have a search box working in my application, however it only returns a result if the search matches exactly what is submitted, as opposed to something like it. Heres my code for the search method;
def self.search(search)
if search
where(:title => ["title LIKE ?", "#{search}"])
else
all
end
end
The "title LIKE ?" doesn't seem to be returning results which are like the query, only ones which are exactly the same.
What am I missing here?
Try this
where(["title LIKE ?", "%#{search}%"])
Here is another way if you want to avoid string queries (using arel):
where(arel_table[:title].matches("%#{search}%"))

Case insensitive and inclusive ActiveRecord queries?

I have views that print out a table of guides that have the word 'farming' in their titles.
def farming
t = Guide.arel_table
#guides = Guide.where(t[:title].matches('%farming'))
end
The problem is I want to show all guides that have at least any kind of spelling of the word 'farming' in it. So "Farming for Dummies" should show up in the search, too.
How do I do this?
In order to match Farming for Dummies you need to use farming%, i.e. % should be after the word farming.
Try the following to return all records with title including the word farming anywhere in title:
def farming
t = Guide.arel_table
#guides = Guide.where(t[:title].matches('%farming%'))
end

Sunspot Rails - get matched substring in fulltext search

i need your help. I want to get the matched string in a full text sunspot search.
Thats how my code looks like at the moment
search2 = WebsiteEntry.search do
fulltext params[:q]
end
search2.each_hit_with_result do |hit, res|
#match = <Here I need your Help, i need the substring on which sunspot made a hit>
#results.push SearchResult.new(res, hit.score, #match)
end
end
Now, if i´m searching for the word "test" sunspot looks for everything where "test" is a substring and case insensitive.
For example: Sunspot return the matched words "FirstTest" or "TEST2" or "testit".
Is it possible to get the matched string?. I need the string on which sunspot hitted
From "FirstTest" in need the hit "Test". Because i would like to display where sunspot founded a match. So, from "FirstTest" i need the substring "Test". From "TEST2" i need "TEST" and so on.
Thank you.
i have now the solution for my problem. The "hit" - Objekt in the "each_hit_with_result" function return a list with the matched substrings, called "highlight"
search2 = WebsiteEntry.search do
fulltext params[:q] do
highlight :domain
end
end
search2.each_hit_with_result do |hit, res|
hit.highlight(:domain).format { |word| puts "highlight: "+word}
No I´m getting each matched string form the full text search. The Problem was, that i didn`t mark my attribute as highlight. Thx

Daisy chaining conditions in Rails controller action.

The index method of my controller looks as follows:
def index
if params["feed_source_id"]
#feeds = Feed.find_all_by_feed_source_id(params["feed_source_id"])
else
#feeds = Feed.all
end
I just added the Metasearch Gem to my app, and it defines the search in my index as:
#search = Feed.search(params[:search])
How can I daisy chain both of these conditions so that #search will perform the search as well as include the find_all_by_feed_source_id condition?
Not sure what you're aiming at - besides doing both searches in the index action? Do you want a union or intersection of both search results?
If you want a union, you could do something like
#feeds = #feeds | #search.all
in addition to all the code above, which would give you a terrible performance.
I had a brief look at the Metasearch Gem, and it should give you the possibility to include the feed_source_id as one of the parameters.
You want the AND of these conditions, right?
I think this should work:
Feed.search({ :feed_source_id_eq => params[:feed_source_id] }.merge(params[:search]))

Rails - Conditional Query, with ActiveRecord?

Given a query like:
current_user.conversations.where("params[:projectid] = ?", projectid).limit(10).find(:all)
params[:projectid] is being sent from jQuery ajax. Sometimes that is an integer and the above works fine. But if the use selects "All Projects, that's a value of '' which rails turns into 0. which yields an invalid query
How with rails do you say search params[:projectid] = ? if defined?
Thanks
I think you may have mistyped the query a bit. "params[:projectid] = ?" shouldn't be a valid query condition under any circumstances.
In any case, you could do some sort of conditional statement:
if params[:project_id].blank?
#conversations = current_user.conversations.limit(10)
else
#conversations = current_user.conversations.where("project_id = ?", params[:project_id]).limit(10)
end
Although, I'd probably prefer something like this:
#conversations = current_user.conversations.limit(10)
#converstaions.where("project_id = ?", params[:project_id]) unless params[:project_id].blank?
Sidenotes:
You don't have to use .find(:all). Rails will automatically execute the query when the resultset is required (such as when you do #conversations.each).
Wherever possible, try to adhere to Rails' snakecasing naming scheme (eg. project_id as opposed to projectid). You'll save yourself and collaborators a lot of headaches in the long run.
Thanks but if the where query has lets say 3 params, project_id, project_status, ... for example, then the unless idea won't work. I'm shocked that Rails doesn't have a better way to handle conditional query params
EDIT: If you have multiple params that could be a part of the query, consider the fact that where takes a hash as its argument. With that, you can easily build a parameter hash dynamically, and pass it to where. Something like this, maybe:
conditions = [:project_id, :project_status, :something_else].inject({}) do |hsh, field|
hsh[field] = params[field] unless params[field].blank?
hsh
end
#conversations = current_user.conversations.where(conditions).limit(10)
In the above case, you'd loop over all fields in the array, and add each one of them to the resulting hash unless it's blank. Then, you pass the hash to the where function, and everything's fine and dandy.
I didn't understand why you put:
where("params[:projectid] = ?", projectid)
if you receive params[:project] from the ajax request, the query string shouldn't be:
where("projectid = ?", params[:projectid])
intead?
And if you are receiving an empty string ('') as the parameter you can always test for:
unless params[:projectid].blank?
I don't think i undestood your question, but i hope this helps.

Resources