Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6916381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:35:43+00:00 2026-05-27T09:35:43+00:00

Our Rails 3 app needs to be able to accept foreign characters like ä

  • 0

Our Rails 3 app needs to be able to accept foreign characters like ä and こ, and save them to our MySQL db, which has its character_set as ‘utf8.’

One of our models runs a validation which is used to strip out all the non-word characters in its name, before being saved. In Ruby 1.8.7 and Rails 2, the following was sufficient:

def strip_non_words(string)
  string.gsub!(/\W/,'')
end

This stripped out bad characters, but preserved things like ‘ä’, ‘こ’, and ‘3.’ With Ruby 1.9’s new encodings, however, that statement no longer works – it is now removing those characters as well as the others we don’t want. I am trying to find a way to do that.

Changing the gsub to something like this:

def strip_non_words(string)
  string.gsub!(/[[:punct]]/,'')
end

lets the string pass through fine, but then the database kicks up the following error:

Mysql2::Error: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation

Running the string through Iconv to try and convert it, like so:

def strip_non_words(string)
  Iconv.conv('LATIN1', 'UTF8', string)
  string.gsub!(/[[:punct]]/,'')
end

Results in this error:

Iconv::IllegalSequence: "こäè" # "こäè" being a test string

I’m basically at my whits end here. Does anyone know of a way to do do what I need?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:35:44+00:00Added an answer on May 27, 2026 at 9:35 am

    This ended up being a bit of an interesting fix.

    I discovered that Ruby has a regex I could use, but only for ASCII strings. So I had to convert the string to ASCII, run the regex, then convert it back for submission to the db. End result looks like this:

    def strip_non_words(string)
      string_encoded = string.force_encoding(Encoding::ASCII_8BIT)
      string_encoded.gsub!(/\p{Word}+/, '') # non-word characters
      string_reencoded = string_encoded.force_encoding('ISO-8859-1')
      string_reencoded #return
    end
    

    Turns out you have to encode things separately due to how Ruby handles changing a character encoding: http://ablogaboutcode.com/2011/03/08/rails-3-patch-encoding-bug-while-action-caching-with-memcachestore/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're hosting our Ruby on Rails application with the EngineYard App Cloud, which has
As I said in a previous post, our Rails app has to interface with
There is a purchasing controller in our rails 3.1.0 app. A purchasing needs 4
Our app has a lease_booking model with a field of lease_date which is a
As part of the deployment process for our rails 2.3 app, I'd like to
Our rails app is designed as a single code base linking to multiple client
We are getting close to releasing our new rails app and so far interest
After our Ruby on Rails application has run for a while, it starts throwing
A user registers on our Rails app and they're given javascript to embed a
In our Rails 3.1.0 app, we need to modify params passed to rfq controller

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.