Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

db:seed failing with multibyte characters

#5748

rake db:seed fails with multibyte characters in Rails 3.0.0.

Example seeds.rb file:

countries = Country.create([
                              {:name => 'Åland Islands', :iso_code => 'AX'},
                              {:name => 'Côte D\'Ivoire', :iso_code => 'CI'}])

This fails on both the Å and ô characters with the following error:

claymac:emr clay$ rake db:seed --trace
(in /Users/clay/rails/emr)
Invoke db:seed (first_time)
Invoke db:abort_if_pending_migrations (first_time)
Invoke environment (first_time)
Execute environment
Execute db:abort_if_pending_migrations
Execute db:seed
rake aborted!
/Users/clay/rails/emr/db/seeds.rb:2: invalid multibyte char (US-ASCII)
/Users/clay/rails/emr/db/seeds.rb:2: invalid multibyte char (US-ASCII)
/Users/clay/rails/emr/db/seeds.rb:2: syntax error, unexpected $end, expecting '}'
...                  {:name => 'Åland Islands', :iso_code => '...
...                               ^
/Users/clay/.rvm/gems/ruby-1.9.2-p0@up_to_date/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:235:in `load'
/Users/clay/.rvm/gems/ruby-1.9.2-p0@up_to_date/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:235:in `block in load'
/Users/clay/.rvm/gems/ruby-1.9.2-p0@up_to_date/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:225:in `block in load_dependency'
/Users/clay/.rvm/gems/ruby-1.9.2-p0@up_to_date/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:591:in `new_constants_in'
/Users/clay/.rvm/gems/ruby-1.9.2-p0@up_to_date/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:225:in `load_dependency'
/Users/clay/.rvm/gems/ruby-1.9.2-p0@up_to_date/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:235:in `load'
/Users/clay/.rvm/gems/ruby-1.9.2-p0@up_to_date/gems/activerecord-3.0.0/lib/active_record/railties/databases.rake:281:in `block (2 levels) in <top (required)>'
/Users/clay/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:636:in `call'
...

FYI, I used shift-option-A on a US keyboard to create the Å.

Reported by ClayH · October 1st, 2010 @ 04:03 PM

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Szymon Nowak
    Szymon Nowak

    Just add the following line as the first line in the file:

    # -*- coding: utf-8 -*-
    

    and it will work.

    You can probably set your editor to do add this line automatically for you, whenever there are some non-ASCII characters in the file.

    October 2nd, 2010 @ 08:51 PM

  2. Rohit Arondekar
    Rohit Arondekar
    • Importance changed from to Low

    ClayH, did the above fix the problem?

    P.S I thought in Ruby 1.9.2 all Ruby files were treated as UTF-8.

    October 6th, 2010 @ 07:21 AM

  3. David Trasbo
    David Trasbo
    • State changed from new to invalid

    Confirmed, having # -*- coding: utf-8 -*- or alternatively # encoding: utf-8 as the first line solves the problem. db/seeds.rb is loaded using Ruby's load method, so ultimately it's a Ruby issue.

    October 6th, 2010 @ 05:29 PM

  4. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 11th, 2010 @ 10:56 AM