Skip to content

Commit

Permalink
Loading of records now uses AREL instead of direct SQL.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdh committed Feb 18, 2012
1 parent 98e9a5d commit d973da4
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/serialization_helper.rb
Expand Up @@ -80,11 +80,13 @@ def self.load_records(table, column_names, records)
return return
end end
columns = column_names.map{|cn| ActiveRecord::Base.connection.columns(table).detect{|c| c.name == cn}} columns = column_names.map{|cn| ActiveRecord::Base.connection.columns(table).detect{|c| c.name == cn}}
quoted_column_names = column_names.map { |column| ActiveRecord::Base.connection.quote_column_name(column) }.join(',') arel_table = Arel::Table.new table
quoted_table_name = SerializationHelper::Utils.quote_table(table)
records.each do |record| records.each do |record|
quoted_values = record.zip(columns).map{|c| ActiveRecord::Base.connection.quote(c.first, c.last)}.join(',') manager = Arel::InsertManager.new Arel::Table.engine
ActiveRecord::Base.connection.execute("INSERT INTO #{quoted_table_name} (#{quoted_column_names}) VALUES (#{quoted_values})") manager.into arel_table
manager.values = Arel::Nodes::Values.new record
column_names.each { |c| manager.columns << arel_table[c.to_sym] }
ActiveRecord::Base.connection.execute manager.to_sql
end end
end end


Expand Down

0 comments on commit d973da4

Please sign in to comment.