问题
After rails console, I can execute the following query:
1.9.3-p551 :001 > ActivityObject.where(:title => "kiketurpis integer aliquet")
And I got a unique answer existed in database. But if I enter:
1.9.3-p551 :002 > ActivityObject.where(:title => '第一个纵纹')
(In double quotes there is a Chinese string.) I got all records in table activity_objects, which means I can not use Chinese string in predicate where.
Also, I can directly query this record using the Chinese string under psql:
vish_development=# select * from activity_objects where title = '第一个纵纹';
So my question is that what should I do for enabling CJK string in ActiveRecord's where or like this?
回答1:
After Frederick Cheung answer me in maillist Ruby on Rails: Talk, I finally found the answer.
Now it works fine!
Because ruby 1.9 does not use utf-8 as default encode:
1.9.3-p551 :001 > __ENCODING__.name
=> "US-ASCII"
For UTF-8, I used the following commands:
bash-3.2$ export LANG=en_US.UTF-8
bash-3.2$ rails c
Loading development environment (Rails 3.2.22)
1.9.3-p551 :001 > __ENCODING__.name
=> "UTF-8"
After that, I get the right record when I run the previous query:
1.9.3-p551 :002 > ActivityObject.where(:title => '第一个纵纹')
ActivityObject Load (29.5ms) SELECT "activity_objects".* FROM "activity_objects" WHERE "activity_objects"."title" = '第一个纵纹'
=> [#<ActivityObject id: 648, created_at: "2015-06-21 14:41:19", updated_at: "2015-06-21 14:41:19", object_type: "Excursion", like_count: 0, title: "第一个纵纹", description: "整的行不?", follower_count: 1, visit_count: 5, language: "independent", age_min: 0, age_max: 0, notified_after_draft: false, comment_count: 0, popularity: 0, download_count: 0, qscore: 500000, reviewers_qscore: nil, users_qscore: nil, ranking: 0, title_length: 1, desc_length: 1, tags_length: 1, scope: 0, avatar_file_name: nil, avatar_content_type: nil, avatar_file_size: nil, avatar_updated_at: nil, teachers_qscore: nil>]
来源:https://stackoverflow.com/questions/30970840/how-to-enable-activerecord-to-support-cjk-query