Guest User

Untitled

a guest
May 7th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. require 'hpricot'
  2. require 'active_record'
  3.  
  4. class AdResult < ActiveRecord::Base
  5. end
  6.  
  7. ActiveRecord::Base.establish_connection(
  8. :adapter=>'mysql',
  9. :database=>'xxxx',
  10. :host=>'localhost',
  11. :username=>'root',
  12. :password=>'xxxx'
  13. )
  14.  
  15. unless AdResult.table_exists?
  16. first_row = rows.first # We'll use this row as a model
  17. # to create the database schema
  18. field_override_types = {
  19. 'imps'=>:integer,
  20. 'clicks'=>:integer,
  21. 'ctr'=>:float,
  22. 'cpc'=>:integer,
  23. 'cost'=>:integer
  24. }
  25.  
  26. ActiveRecord::Schema.define do
  27. create_table :ad_results do |t|
  28. first_row.attributes.each do |attribute_name, value|
  29. if field_override_types.include?(attribute_name)
  30. t.column attribute_name, field_override_types[attribute_name]
  31. else
  32. t.column attribute_name, :text, :length=>25
  33. end
  34. end
  35. end
  36. end
  37. end
  38.  
  39. hpricot_doc = Hpricot.XML(ARGF)
  40. rows = (hpricot_doc/"rows/row")
  41.  
  42. rows.each do |row|
  43. AdResult.new do |n|
  44. row.attributes.each do |attribute_name, attribute_value|
  45. n.send("#{attribute_name}=", attribute_value)
  46. end
  47. n.save
  48. end
  49. end
Add Comment
Please, Sign In to add comment