Permalink
Browse files

Loading of records now uses AREL instead of direct SQL.

  • Loading branch information...
1 parent 98e9a5d commit d973da4204d2fcc4a657f69058a8c3cfcd5806ed @mdh committed Feb 18, 2012
Showing with 6 additions and 4 deletions.
  1. +6 −4 lib/serialization_helper.rb
@@ -80,11 +80,13 @@ def self.load_records(table, column_names, records)
return
end
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(',')
- quoted_table_name = SerializationHelper::Utils.quote_table(table)
+ arel_table = Arel::Table.new table
records.each do |record|
- quoted_values = record.zip(columns).map{|c| ActiveRecord::Base.connection.quote(c.first, c.last)}.join(',')
- ActiveRecord::Base.connection.execute("INSERT INTO #{quoted_table_name} (#{quoted_column_names}) VALUES (#{quoted_values})")
+ manager = Arel::InsertManager.new Arel::Table.engine
+ 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

0 comments on commit d973da4

Please sign in to comment.