# Rails & sqlserver collation problems

**URL:** <https://www.ruby-forum.com/t/rails-sqlserver-collation-problems/203605>\
**Category:** Rails\
**Created:** [February 23, 2011, 6:02pm UTC](https://www.ruby-forum.com/t/rails-sqlserver-collation-problems/203605 "2011-02-23T18:02:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 23, 2011, 6:02pm UTC](https://www.ruby-forum.com/t/rails-sqlserver-collation-problems/203605/1 "2011-02-23T18:02:36Z")

</div>

Boa tarde pessoal.

Estou utilizando o intant rails com ruby v.1.8.6 e rails 2.0.2 com um  
servidor sqlserver2000, minha base de dados usa o collation  
SQL\_latin1\_general\_CP1\_CI\_AS, quando tragos strings com acentuao e  
cedilha no consigo visualizar os carateres corretos, sei que para  
exibir corretamente o texto no ruby tenho que usar UTF-8. Como posso  
fazer pra mudar o collation da minha base ou converter as string para  
UTF-8 na hora de exibir?

Desde j agradeo  
Eduardo

---

<div class="post-metadata">

**Author:** ![Edu\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e19b73/32.png) [@Edu\_M](https://www.ruby-forum.com/u/Edu_M)\
**Post date:** [February 23, 2011, 7:17pm UTC](https://www.ruby-forum.com/t/rails-sqlserver-collation-problems/203605/2 "2011-02-23T19:17:12Z")

</div>

Peo desculpas pelo meu Portugus. Estou usando o Google Translate. 🙂

Voc precisa informar o arquivo database.yml que pretende conversar  
com o banco de dados em UTF-8. Para fazer isso adicione encoding: utf8  
para o seu banco de dados no database.yml. Deixe-me saber como isso  
vai.

Obrigado,  
B.

2011/2/23 Edu M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![Edu\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e19b73/32.png) [@Edu\_M](https://www.ruby-forum.com/u/Edu_M)\
**Post date:** [February 23, 2011, 11:44pm UTC](https://www.ruby-forum.com/t/rails-sqlserver-collation-problems/203605/3 "2011-02-23T23:44:09Z")

</div>

On Wed, Feb 23, 2011 at 1:21 PM, Michael P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Por favor, pare top postagem

> **[Posting style](https://en.wikipedia.org/wiki/Posting_style)**
>
> In text-based internet communication, a posting style is the manner in which earlier messages are included or quoted. The concept applies to formats such as e-mail, Internet forums and Usenet. The main options are interleaved posting (also called inline replying, in which the different parts of the reply follow the relevant parts of the original post), bottom-posting (in which the reply follows the quote) or top-posting (in which the reply precedes the quoted original message). For each of those ...

“The traditional style was for a long time to post the answer below as  
much  
of the quoted original as was necessary to understand the reply (bottom  
or  
inline). _Many years later, when email became widespread in business  
communication, it however became a widespread policy to reply above the  
entire original and leave it untouched (and without any prefixes at the  
beginning of each line) below the reply._”

---

<div class="post-metadata">

**Author:** ![Edu\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e19b73/32.png) [@Edu\_M](https://www.ruby-forum.com/u/Edu_M)\
**Post date:** [February 23, 2011, 8:23pm UTC](https://www.ruby-forum.com/t/rails-sqlserver-collation-problems/203605/4 "2011-02-23T20:23:03Z")

</div>

2011/2/23 Bryan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Peo desculpas pelo meu Portugus. Estou usando o Google Translate. 🙂

Por favor, pare top postagem

---

<div class="post-metadata">

**Author:** ![Edu\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e19b73/32.png) [@Edu\_M](https://www.ruby-forum.com/u/Edu_M)\
**Post date:** [February 23, 2011, 11:52pm UTC](https://www.ruby-forum.com/t/rails-sqlserver-collation-problems/203605/5 "2011-02-23T23:52:12Z")

</div>

A: Because it messes up the order in which people normally read text.  
Q: Why is top-posting such a bad thing?  
A: Top-posting.  
Q: What is the most annoying thing in e-mail?

On 23 February 2011 22:42, Bryan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On Wed, Feb 23, 2011 at 1:21 PM, Michael P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Por favor, pare top postagem
> 
> “The traditional style was for a long time to post the answer below as much  
> of the quoted original as was necessary to understand the reply (bottom or  
> inline). Many years later, when email became widespread in business  
> communication, it however became a widespread policy to reply above the  
> entire original and leave it untouched (and without any prefixes at the  
> beginning of each line) below the reply.”

This list isn’t a business communication - it’s a reference resource,  
and that “policy” was imposed because one company’s email software  
that just “did it that way” - irregardless of any user preference.

> [Posting style - Wikipedia](http://en.wikipedia.org/wiki/Posting_style)

Read the whole of that article… especially the section on “Choosing  
the proper posting style”. If after that you still insist on  
top-posting, so be it :-/  
_shrug_

---

<div class="post-metadata">

**Author:** ![Edu\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e19b73/32.png) [@Edu\_M](https://www.ruby-forum.com/u/Edu_M)\
**Post date:** [February 24, 2011, 12:17am UTC](https://www.ruby-forum.com/t/rails-sqlserver-collation-problems/203605/6 "2011-02-24T00:17:51Z")

</div>

I reply to the email that I receive which uses the standard method of  
top  
posting. Why? Because if you bottom-posted all the time or unnecessarily  
inline quoted it would make this thread:  
[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/6c25db52c2d743aaand](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/6c25db52c2d743aaand)  
others like it unreadable.

I’m done discussing posting styles. Let’s return to helping each other  
out  
which is the main reason for this group.

---

<div class="post-metadata">

**Author:** ![Edu\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e19b73/32.png) [@Edu\_M](https://www.ruby-forum.com/u/Edu_M)\
**Post date:** [February 24, 2011, 12:22am UTC](https://www.ruby-forum.com/t/rails-sqlserver-collation-problems/203605/7 "2011-02-24T00:22:14Z")

</div>

On 23 February 2011 23:07, Bryan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I reply to the email that I receive which uses the standard method of top  
> posting.

What “standard”? Which RFC says that there is any “standard”?

> if you … unnecessarily  
> inline quoted it would make this thread:

> **[rails & sqlserver collation problems](https://groups.google.com/g/rubyonrails-talk/c/bCXbUsLXQ6o)**

> and others like it unreadable.

no… top-posting makes it impossible to maintain a coherent  
conversation, and introduces confusion about what replies refer to,  
because there’s no context.

> I’m done discussing posting styles.

So I guess you _didn’t_ bother reading the rest of the article you  
waved as defence of your position? … hence my _shrug_

PS You’ve got a frightfully annoying email client too, which sends  
_to_ the person, and CCs the group - so further replies go to you, not  
the group… :-/

---

<div class="post-metadata">

**Author:** ![Edu\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e19b73/32.png) [@Edu\_M](https://www.ruby-forum.com/u/Edu_M)\
**Post date:** [February 24, 2011, 1:50pm UTC](https://www.ruby-forum.com/t/rails-sqlserver-collation-problems/203605/8 "2011-02-24T13:50:11Z")

</div>

Eu tentei usar o encoding: utf8 no database.yml, mas para o sql server  
não funciona. tentei então um helper para a classe string com o  
seguinte código

-----------------------------------------------------------code starts  
here-----------------------------------------------------------------------

class String

# Returns a valid DateTime object if the object can be converted

according to +format+, nil otherwise.

# FIXME: Corrigir o erro de validação em colunas contendo data +

hora.

gem ‘chardet’  
require ‘UniversalDetector’

def parse\_date(format =  
ActiveSupport::CoreExtensions:📅:Conversions::DATE\_FORMATS[:default])  
begin  
DateTime.strptime(self, format)  
rescue ArgumentError  
end  
end

def to\_utf8  
@encode\_type = UniversalDetector::chardet(self)[“encoding”]  
#detects the str encoding  
Iconv.iconv(“UTF-8”, @encode\_type, self).to\_s #converts the  
current encoding to UTF-8 of the present string  
end

end

-----------------------------------------------------------code ends  
here-----------------------------------------------------------------------

O chardet é uma gem que pode retornar o tipo de codificação utilizada  
na string, o tipo de codificação retornado para o  
SQL\_latin1\_genreal\_CP1\_CI\_AS é o ISO-8859-5 e o iconv consegue passar  
a string para utf8, porém os resultados ainda não são satisfatórios  
pois os caracteres acentuados e as cedilhas não são exibidos  
corretamente. Veja abaixo:

O que deveria exibir:

Fundação Ecumênica de Proteção ao Excepcional - PARANC

está exibindo:

Fundaчуo Ecumъnica de Proteчуo ao Excepcional - PARANС

Sinto que estou chegando perto, alguém sabe como prosseguir deste  
ponto?

Desde já agradeço  
Edu
