Luc
Luc

Reputation: 17082

Cannot select column with accent in rails

I have the Level record in my rails app, it contains 1 field (name) that can contain chars with accents.

1.9.3p125 :008 > Level.all
 Level Load (0.4ms)  SELECT "levels".* FROM "levels" 
 => [#<Level id: 1, name: "Débutant">, #<Level id: 2, name: "Intermédiaire">, #<Level id: 3, name: "Avancé">]  

But when I query, I have:

1.9.3p125 :011 > Level.where("name = ?", "D\U+FFC3\U+FFA9butant").first
 Level Load (0.3ms)  SELECT "levels".* FROM "levels" WHERE (name = 'Dbutant') LIMIT 1
 => nil 

I cannot type Level.where("name = ?", "Débutant").first when using rails c as é is directly replaced by \U+FFC3\U+FFA9. But in my controller, the result is the same, I cannot query accentuated string.

I currently use sqlite for my tests.

Upvotes: 0

Views: 279

Answers (2)

severin
severin

Reputation: 10268

Your problem in the console is related to the readline library: You have to install Ruby with proper readline support.

If you have installed Ruby using rvm, the quickest way to resolve this problem is:

rvm pkg install readline

and then (assuming you are using 1.9.3, otherwise just replace with your Ruby version):

rvm reinstall 1.9.3 --with-readline-dir=$rvm_path/usr

After that, typing é in the Rails console should work again...

Regarding your other problem (in the controller): Could you please post a log excerpt containing the SQL query that is being made?

Upvotes: 1

denis.peplin
denis.peplin

Reputation: 9851

Place this string

#encoding: utf-8

to the first line of your Ruby file.

Upvotes: 0

Related Questions