Rails 3 + Postgre + incompatible character encodings: ASCII-8BIT and UTF-8

Hi friends!
I’m having problems to load some data in a Postgresql database using
“rake db:seed”.

/work/arzamas1$ rake db:seed
(in /home/luiz/work/arzamas1)
rake aborted!
incompatible character encodings: ASCII-8BIT and UTF-8

The database was created with these parameters:
CREATE DATABASE arzamas1
WITH OWNER = postgres
ENCODING = ‘UTF8’
TABLESPACE = pg_default
LC_COLLATE = ‘pt_BR.utf8’
LC_CTYPE = ‘pt_BR.utf8’
CONNECTION LIMIT = -1;

This is my database.yml:
development:
adapter: postgresql
encoding: utf8
database: arzamas1
username: postgres
password: postgres

This is my gemfile:
source ‘http://rubygems.org
gem ‘rails’, ‘3.0.3’
gem ‘pg’ <---- (0.10.1)
gem ‘jquery-rails’
group :development do
gem ‘rspec-rails’
gem ‘annotate-models’
end

The classes related with the problem have the following definition (I
suppressed some parameters for legibility):

class CreateNames < ActiveRecord::Migration
def self.up
create_table :names do |t|
t.string :name, :null=>false
end
add_index :names, :name, :unique => true
end

class Name < ActiveRecord::Base
has_many :city_names
has_many :cities, :through => :city_names

validates :name, :presence => true, :uniqueness => { :case_sensitive
=> false }
# Attention! Brasil = brasil
end

class CreateCities < ActiveRecord::Migration
def self.up
create_table :cities do |t|
t.references :countries, :null=>false
t.references :states
t.references :observations
end
add_index :cities, [:countries_id, :states_id], :name =>
‘cities_index’, :unique => false
end

class City < ActiveRecord::Base
has_one :person
has_many :city_names
end

class CreateCityNames < ActiveRecord::Migration
def self.up
create_table :city_names do |t|
t.references :cities, :null=>false
t.references :names, :null=>false
t.string :idiom, :null=>false, :default=>‘pt’, :limit=>2
t.boolean :use_this, :default=>true
end
add_index :city_names, [:cities_id, :names_id], :name =>
‘city_names_index’, :unique => true
end

class CityName < ActiveRecord::Base
belongs_to :cities
belongs_to :names
end

class CreatePeople < ActiveRecord::Migration
def self.up
create_table :people do |t|

t.string :sexo_nasc, :null=>false, :default=>‘i’, :limit=>1 #
(m)masculino, (f)feminino, (i)indeterminado
t.references :infodates
t.references :typedates
t.references :cities # naturalidade
t.references :countries # nacionalidade
end
end

class Person < ActiveRecord::Base
belongs_to :cities # naturalidade
belongs_to :countries # nacionalidade
end

And this is the seeds.rb file:

coding: utf-8

Name.create(:name => ‘Brasil’)
Name.create(:name => ‘So Paulo’)
Country.create(:sgl_country => ‘BR’)
CountryName.create(:countries_id => Country.find(:first, :conditions
=> “sgl_country = ‘BR’”).id, :names_id =>
Name.find(:first, :conditions => “name = ‘Brasil’”).id, :use_this =>
true)
City.create(:countries_id => Country.find(:first, :conditions =>
“sgl_country = ‘BR’”).id,:states_id => State.find(:first, :conditions
=> “sgl_state = ‘SP’”).id)
CityName.create(:cities_id => City.find(:last).id, :names_id =>
Name.find(:first, :conditions => “name = ‘So Paulo’”).id)
Infodate.create(:data_i => ‘1959-04-25’)

Person.create(:sexo_nasc => ‘m’, :infodates_id =>
Infodate.find(:first, :conditions => “data_i = ‘1959-04-25’ and data_f
is null”).id, :typedates_id => Typedate.find(:first, :conditions =>
“usar_para = ‘people’”).id,
:cities_id => CityName.find(:cities_id, :joins => “inner join names on
city_names.names_id = names.id”, :conditions => “names.name = ‘So
Paulo’”))

The problem comes from this last column attrib - “:cities_id =>
CityName.find(…”. incompatible character encodings: ASCII-8BIT and
UTF-8.

If I replace the name ‘So Paulo’ with another city whose name doesn’t
have accent, the error changes to:

rake aborted!
PGError: ERRO: sintaxe de entrada invlida para integer:
“cities_id” (…invalid syntax for integer…)
LINE 1: …mes.name = ‘Planaltina’) AND (“city_names”.“id” =
'cities_id…
^
: SELECT “city_names”.* FROM “city_names” inner join names on
city_names.names_id = names.id WHERE (names.name = ‘Planaltina’) AND
(“city_names”.“id” = ‘cities_id’) LIMIT 1

I googled a lot but now I really got stuck in the mud :-). Any help
will be very appreciated.

try this on the top of you seed.rb:

-- coding: utf-8 --

Hmmm, I tried but didn’t worked.
Thanks for reply!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs