Database.yml: encoding: utf8 does not work

Hi all

According to the Agile 2nd Ed book I added the line encoding: utf8 to my
database.yml connections.

But now when running rake I get plenty of errors:

Character set ‘utf-8’ is not a compiled character set and is not
specified in the ‘/usr/local/mysql/share/mysql/charsets/Index’ file

What’s wrong here?
I checked this Index file, but it seems to be empty…

Thanks a lot
Josh

Hi, you should be able to add the encoding as follows to the
database.yml file:

encoding: UTF8

Let me know if this works for you.

-Conrad

Hi, it’s allways helpful to supply any relevant files when asking a
question about an error that one is receiving.

-Conrad

there are several places that can cause problems with utf-8 encoding.
but some tricks are to:

  1. make sure that every file in your project is utf-8 based (if you
    are using rad rails, this is simple to accomplish: mark your project,
    select properties, in the “text-file-encoding” box, select “other:
    utf-8”)

Be sure to put in your strange “å,ä,ö” characters in your files again
or you’ll get a mysql error, because it will change your “å,ä,ö” to a
“square” (unknown character)

  1. in your databases.yml set for each server environment (in this
    example “development” with mysql)

development:
adapter: mysql
encoding: utf8

  1. set a before filter in your application controller
    (application.rb):

class ApplicationController < ActionController::Base
before_filter :set_charset
def set_charset
@headers[“Content-Type”] = “text/html; charset=utf-8”
end
end

  1. be sure to set the encoding to utf-8 in your mysql (I’ve only used
    mysql… so I don’t know about other databases) for every table. If you
    use mySQL Administrator you can do like this: edit table, press the
    “table option” tab, change charset to “utf8” and collation to
    “utf8_general_ci”

uhmm… that’s all I can think of right now.

Well, I tested this code from http://dev.rubyonrails.org/ticket/2404 and
it works for me:

class Fixture
attr_reader :class_name
end

class Fixtures
@@inserted_fixture_list ||= {}
alias :original_insert_fixtures :insert_fixtures

def insert_fixtures
return if @@inserted_fixture_list[values[0].class_name]
@@inserted_fixture_list[values[0].class_name] = true
unless ActiveRecord::Base.connection.select_one(“select 1 from
#{fixture_class_to_table_name(values[0].class_name)}”)
original_insert_fixtures
end
end

def delete_existing_fixtures() end

compute a fixture’s table name from its (known) class name

def fixture_class_to_table_name(class_name)
Inflector::tableize(class_name)
end
end

But I hate it to change Rails’ default behavior with code I don’t really
understand and might disturb Rails in a later release… :-/

Oh I’m very sorry, this reply was meant for another thread…

http://www.ruby-forum.com/topic/103919

Joshua M. wrote:

Hi all

According to the Agile 2nd Ed book I added the line encoding: utf8 to my
database.yml connections.

But now when running rake I get plenty of errors:

Character set ‘utf-8’ is not a compiled character set and is not
specified in the ‘/usr/local/mysql/share/mysql/charsets/Index’ file

What’s wrong here?
I checked this Index file, but it seems to be empty…

Thanks a lot
Josh

You have to make sure your database is created to utf8 as the default
character set.

Quoting Edmond C. [email protected]:

What’s wrong here?
I checked this Index file, but it seems to be empty…

Thanks a lot
Josh

You have to make sure your database is created to utf8 as the default
character set.

And that the encoding is called utf-8. In my config/database.yml I
have:

encoding: utf8

Also check the default and/or table(s) character set and collation in
MySQL.

Jeffrey