Question

The app that I'm currently working on has a search functionality. When searching for a specific string for e.g,. "4 for £3" which matches a particular column of a table row, the search doesn't return any results although the backend DB has correctly stored that as the value in the column for a particular row.

The log passes the search string as "4 for £3". I'm not sure from where does the additional 'Â' character get appended to my search string(search_str). I tried the following workaround to search for the exact string from the backend but it doesn't seem to work -

Workaround -

params[:search_str].include?('Â') ? params[:search_str].gsub!('£', '£') : 

Also, just for additional info when say trying to same thing in IRB, I get the following -

irb(main):020:0> a = "2 for £7"
=> "2 for \302\2437"
irb(main):021:0>

irb(main):013:0> a.include?('Â')
=> true

The same command when executed in Rails, returns false when calling the action from the search button clicked from the view file. Not sure why this abnormal behavior, is it probably because Rails treats "2 for £7" as "2 for £7" and not "2 for \302\2437" ?

I verified that '\302' and '\243' are the octal equivalents as given in this page. I also did have a look at a similar question, but it didn't seem to help my case.

Updated Question

The database.yml is -

development:
  adapter: mysql
  database: db_name 
  username: 
  password: 
  host: localhost 

dev-rails:
  adapter: mysql
  database: 
  username: 
  password: 
  host: 
  port: 

dev-delayedjobs:
  development

test:
  adapter: mysql
  database: db_name
  username: 
  password:  
  host: 
  port:  

Results returned as part of the query - show variables like 'char%';

character_set_client utf8

character_set_connection utf8

character_set_database latin1

character_set_filesystem binary

character_set_results utf8

character_set_server latin1

character_set_system utf8

character_sets_dir /app(.. some path..)

Was it helpful?

Solution

It sounds like your database is storing data in utf8 and rails is translating it using a different character set (probably latin1). Add this line to your development section in your config/database.yml file:

encoding: utf8

then restart your rails server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top