Created
April 22, 2014 18:01
-
-
Save altherlex/11188637 to your computer and use it in GitHub Desktop.
Solution for IBM_DB in ISO-8859-1 - Problem with special character when update a object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Overwrite update method from active_record/persistence.rb | |
# Solution for IBM_DB in ISO-8859-1 - Problem with special character when update a object | |
module ActiveRecord | |
module Persistence | |
def update(attribute_names = @attributes.keys) | |
klass = self.class | |
#No composite primary key | |
if !self.composite? | |
attributes_with_values = arel_attributes_values(false, false, attribute_names) | |
return 0 if attributes_with_values.empty? | |
id_reg = klass.arel_table[klass.primary_key].eq(id) | |
else | |
attributes_with_values = arel_attributes_values(can_change_primary_key?, false, attribute_names) | |
return 0 if attributes_with_values.empty? | |
if !can_change_primary_key? and primary_key_changed? | |
raise ActiveRecord::CompositeKeyError, "Cannot update primary key values without ActiveModel::Dirty" | |
elsif primary_key_changed? | |
id_reg = primary_key_was | |
else | |
id_reg = ids_hash | |
end | |
end | |
# Recuperando columns: [ActiveRecord::ConnectionAdapters::IBM_DBColumn, value] | |
#col_definitions = attributes_with_values.inject([]) do |ac,v| | |
# ac<<[klass.columns.find{|x| x.name==v.first.name}, v.last] | |
#end | |
column_hash = klass.connection.schema_cache.columns_hash klass.table_name | |
db_columns_with_values = attributes_with_values.map { |attr,value| | |
real_column = column_hash[attr.name] | |
[real_column, value] | |
} | |
bind_attrs = attributes_with_values.dup | |
baloes = [] | |
bind_attrs.keys.each_with_index do |column, i| | |
real_column = db_columns_with_values[i].first | |
bind_attrs[column] = klass.connection.substitute_at(real_column, i) | |
end | |
stmt = klass.unscoped.where(id_reg).arel.compile_update(bind_attrs) | |
klass.connection.update stmt.to_sql.gsub("'?'", "?"), 'SQL', db_columns_with_values | |
rescue | |
klass.connection.update stmt.to_sql | |
end | |
end #Persistence | |
end #ActiveRecord | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment