I'm working with a legacy database in Oracle where the database encode is set to "UTF-8", but the tables can be in any encoding for example: "Windows-1252", "Latin1" etc, due to keep compatibility with legacy software. For now we are migrating those systems to Rails but meanwhile they have to coexist simultaneously. In my specific case the tables that i'm working with are in "Windows-1252".
This is how my database.yml is set up:
default: &default
adapter: oracle_enhanced
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: username
password: password
development:
<<: *default
database: development_database
encoding: utf8
The problem is that whenever I receive an ActiveRecord Relation object and I want to show it I have to manually loop through it and convert "Windows-1252" to "UTF-8" encoding using the encode Ruby method.
For example let's suppose a table User with only two attributes id and username, the related code would look like this in the view:
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.id.encode('UTF-8', 'Windows-1252', invalid: :replace) %></td>
<td><%= user.username.encode('UTF-8', 'Windows-1252', invalid: :replace)%></td>
</tr>
<% end %>
</tbody>
This is not the only problem, when I need to generate JSON from the ActiveRecord Relation object I get this error JSON :: GeneratorError: source sequence is illegal / malformed utf-8
, and many others depending on what I want to do.
The question is: is there a way to set the enconding of a table right in the model, in the given example: user.rb
, and make rails automatically convert the original encoding regardless of which it is to another like "UTF8", in a way kind of the used in database.yml, only this time the configuration is not for the whole database but in a per table way?
This certainly would clear my code a lot and would help a lot in the development process.