Class: TempTable::Insert
- Inherits:
-
Object
- Object
- TempTable::Insert
- Defined in:
- lib/temp_table/insert.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#table_name ⇒ Object
Returns the value of attribute table_name.
Instance Method Summary collapse
-
#initialize(table_name, data) ⇒ Insert
constructor
A new instance of Insert.
- #perform ⇒ Object
Constructor Details
#initialize(table_name, data) ⇒ Insert
Returns a new instance of Insert.
7 8 9 10 11 |
# File 'lib/temp_table/insert.rb', line 7 def initialize(table_name, data) super() @table_name = table_name @data = data end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
5 6 7 |
# File 'lib/temp_table/insert.rb', line 5 def data @data end |
#table_name ⇒ Object
Returns the value of attribute table_name.
5 6 7 |
# File 'lib/temp_table/insert.rb', line 5 def table_name @table_name end |
Instance Method Details
#perform ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/temp_table/insert.rb', line 13 def perform data.each do |row| column_names = row.keys.join(", ") values = row.values.map do |value| if value.is_a?(Array) ActiveRecord::Base.connection.quote(value.to_json) else ActiveRecord::Base.connection.quote(value) end end ActiveRecord::Base.connection.execute <<-SQL.squish INSERT INTO #{@table_name} (#{column_names}) VALUES (#{values.join(', ')}); SQL end end |