Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'hpricot'
- require 'active_record'
- class AdResult < ActiveRecord::Base
- end
- ActiveRecord::Base.establish_connection(
- :adapter=>'mysql',
- :database=>'xxxx',
- :host=>'localhost',
- :username=>'root',
- :password=>'xxxx'
- )
- unless AdResult.table_exists?
- first_row = rows.first # We'll use this row as a model
- # to create the database schema
- field_override_types = {
- 'imps'=>:integer,
- 'clicks'=>:integer,
- 'ctr'=>:float,
- 'cpc'=>:integer,
- 'cost'=>:integer
- }
- ActiveRecord::Schema.define do
- create_table :ad_results do |t|
- first_row.attributes.each do |attribute_name, value|
- if field_override_types.include?(attribute_name)
- t.column attribute_name, field_override_types[attribute_name]
- else
- t.column attribute_name, :text, :length=>25
- end
- end
- end
- end
- end
- hpricot_doc = Hpricot.XML(ARGF)
- rows = (hpricot_doc/"rows/row")
- rows.each do |row|
- AdResult.new do |n|
- row.attributes.each do |attribute_name, attribute_value|
- n.send("#{attribute_name}=", attribute_value)
- end
- n.save
- end
- end
Add Comment
Please, Sign In to add comment