mysql encoding with rails 2.3.2 and ruby 1.9.1

121 views
Skip to first unread message

ruby.freeman

unread,
Mar 21, 2009, 10:52:44 AM3/21/09
to Ruby on Rails: Talk
when I run console with ruby 1.9.1 and rails 2.3.2, and trying to do
something like

User.first.name.encoding

I'm getting #<Encoding:ASCII-8BIT>, though I've set "encoding: utf8"
in database.yml

any suggestions?

Jeremy Kemper

unread,
Mar 21, 2009, 11:18:14 PM3/21/09
to rubyonra...@googlegroups.com

The mysql driver is not encoding-aware yet.

jeremy

Stefan La

unread,
Mar 23, 2009, 1:16:07 PM3/23/09
to rubyonra...@googlegroups.com

Same problem here with mysql and sqlite3.

When will the mysql and sqlite3 drivers be encoding-aware?

Is there a workaround?

I have problems with this because I'm from germany and need utf8 for the
german umlauts.

If i save the templates as ANSI, the outputs from database with umlauts
are working, but when i use umlauts directly in the templates i get this
error:

invalid byte sequence for encoding "ASCII-8BIT"

If i save the templates as UTF8, the umlauts in the templates are
working, but the outputs from the database with umlauts cause this
error:

invalid byte sequence for encoding "UTF-8"


I'm using Ruby 1.9.1, Rails 2.3.2, mysql/ruby 2.8.1, sqlite3/ruby 1.2.4

slang17
--
Posted via http://www.ruby-forum.com/.

Conrad Taylor

unread,
Mar 23, 2009, 2:54:35 PM3/23/09
to rubyonra...@googlegroups.com
Your question should also be posted to driver maintainers and possible the
MySQL and SQLite projects.  Furthermore, all these projects are open-source.
Thus, please feel free to contribute/add these features if you need them.

Good luck,

-Conrad

ruby.freeman

unread,
Apr 4, 2009, 12:10:33 PM4/4/09
to Ruby on Rails: Talk

> Your question should also be posted to driver maintainers and possible the
> MySQL and SQLite projects.  Furthermore, all these projects are open-source.
> Thus, please feel free to contribute/add these features if you need them.

It's problem of rails, not mysql library. ActiveRecord should be
encoding-aware now

Stefan

unread,
Apr 6, 2009, 5:57:16 AM4/6/09
to rubyonra...@googlegroups.com
ruby.freeman wrote:

> It's problem of rails, not mysql library. ActiveRecord should be
> encoding-aware now

Where did you get that information?
Is there any workaround or patch for that problem with ActiveRecord?

ruby.freeman

unread,
Apr 6, 2009, 6:42:29 AM4/6/09
to Ruby on Rails: Talk
> Where did you get that information?
> Is there any workaround or patch for that problem with ActiveRecord?

See, when I do some requests from rails console, I see that result is
in utf-8 encoding, so mysql works correctly. But ActiveRecord sets
encoding for all attributes as ASCII-8BIT. For now we can reencode all
results in our views with "force_encoding" methods untill AcriveRecord
is encoding-aware

Conrad Taylor

unread,
Apr 6, 2009, 8:50:51 AM4/6/09
to rubyonra...@googlegroups.com
Which requests do you perform in the rails console?

-Conrad

 

ruby.freeman

unread,
Apr 6, 2009, 9:00:44 AM4/6/09
to Ruby on Rails: Talk
> Which requests do you perform in the rails console?

any find request, like in first message

Hector Gomez

unread,
Apr 10, 2009, 8:16:45 PM4/10/09
to Ruby on Rails: Talk
I made a bug in lighhouse to track this problems, I have some patches
for mysql and for the problem with the views in a related ticket
https://rails.lighthouseapp.com/projects/8994/tickets/2476-ascii-8bit-encoding-of-query-results-in-rails-232-and-ruby-191#ticket-2476-2

Rick

unread,
Apr 11, 2009, 2:07:18 PM4/11/09
to Ruby on Rails: Talk
Postgresql v6.3.8.1 works with Ruby 1.9.1p0 and Rails 2.3.2 - my guess
is the problem is with either the driver or the database itself.

Can you tell what the actual encoding is of the database? How about
setting the default? MySQL refers to this as the CHARACTER SET. For
example, to set utf8 as the encoding for a table named
"test_development" you could use:

ALTER TABLE `test_development`.`pages` CHARACTER SET utf8;

Noodle around with the MySQL user's manual and I'm sure you'll find
the way to make utf8 the default for all new tables.

For reference, I've got:

mysql Ver 14.14 Distrib 5.1.31, for apple-darwin9.5.0 (powerpc) using
readline 5.1

and

gem list mysql == mysql (2.8.1)




On Apr 10, 2:16 pm, Hector Gomez <hector...@gmail.com> wrote:
> I made a bug in lighhouse to track this problems, I have some patches
> for mysql and for the problem with the views in a related tickethttps://rails.lighthouseapp.com/projects/8994/tickets/2476-ascii-8bit...

ruby.freeman

unread,
Apr 11, 2009, 2:28:33 PM4/11/09
to Ruby on Rails: Talk
> Postgresql v6.3.8.1 works with Ruby 1.9.1p0 and Rails 2.3.2 - my guess
> is the problem is with either the driver or the database itself.

Problem is not with database. As I said above, when I do something
like User.first.name.encoding I'm getting #<Encoding:ASCII-8BIT>.
If I do this one - User.first.name.force_encoding("utf-8") - I'm
getting normal utf-8 string. The problem is that ActiveRecord gets all
data from database as ASCII.

Rick

unread,
Apr 11, 2009, 2:44:55 PM4/11/09
to Ruby on Rails: Talk
right - somewhat slow on the uptake

Hector Gomez

unread,
Apr 11, 2009, 4:51:15 PM4/11/09
to Ruby on Rails: Talk
ruby.freeman have you checked my fork of mysql-ruby? and the ticket
in lighthouse?
I think this will resolve your problem.
Or have you checked it out and still got problems?

Conrad Taylor

unread,
Apr 11, 2009, 6:31:21 PM4/11/09
to rubyonra...@googlegroups.com
Is your update forked back into the main branch for the project?

-Conrad

Hector Gomez

unread,
Apr 11, 2009, 8:07:12 PM4/11/09
to Ruby on Rails: Talk
I have not contacted upstream (tmtm) but he is working on a pure ruby
adapter called ruby-mysql (is encoding aware, etc) it has a compat
layer but is
not functional now.
I have made a gem of my fork hectoregm-mysql-ruby that you
use to make tests, but I will try to get this changes merge upstream.

On Apr 11, 5:31 pm, Conrad Taylor <conra...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages