Could som开发者_StackOverflow社区e on help me to turn the following search into case - insensitive?
Here is the piece of code for earching "title" field in event module:
# Add each field to the conditions array
searchable_fields.each_pair do |field, value|
conditions << "#{field} LIKE ?"
values << "%#{value}%"
end
Here is the data I have entered:
Concert
■Posted by: bancova
■2010-03-14
■boston
test
the "Concert" is the title of this event.
now, when I entered "concert" (small c), I cannot get the event.
however, when I entered "Concert", or "oncert", or "cert"...I can get it.
Could some some friend explain the code and teach me how to make it case insensive?
thanks.
I'm unfamiliar with the tutorial you're using but it looks like it's a database problem, not a Ruby/Rails problem. The problem is that your database is case sensitive so 'Concert' matches because that's what's in the DB, but 'concert' doesn't because it's not an actual match with 'Concert'.
Anyway, the actual solution will depend on your database and how it's configured but lets assume it's MySQL, then your solution would look like this
searchable_fields.each_pair do |field, value|
conditions << "#{field} LIKE LOWER(?)"
values << "%#{value.downcase}%"
end
value.downcase will change the input string to all lowercase and the LOWER sql function will do the same on the database side. They should now match. IF you're using SqlLite or Postgres you'll need to look up their lowercase functions but the rest will still be the same.
精彩评论