2

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
ginny1280
  • 35
  • 4

1 Answers1

3

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

NM Pennypacker
  • 6,704
  • 11
  • 36
  • 38