Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. require 'active_record'
  2. require 'tiny_tds'
  3. require 'activerecord-sqlserver-adapter'
  4. require 'pp'
  5.  
  6. ActiveRecord::Base.establish_connection(
  7. :adapter=> "sqlserver",
  8. :host => "localhost",
  9. :username => "sa",
  10. :password => "your_password"
  11. )
  12.  
  13. #Create new database SampleDB
  14. puts "Drop and create new database 'SampleDB'"
  15. ActiveRecord::Base.connection.drop_database('SampleDB') rescue nil
  16. ActiveRecord::Base.connection.create_database('SampleDB')
  17.  
  18. #Create a new table called Tasks
  19. ActiveRecord::Schema.define do
  20. create_table :tasks, force: true do |t|
  21. t.string :taskname
  22. t.string :user
  23. t.date :duedate
  24. end
  25. end
  26.  
  27. class Task < ActiveRecord::Base
  28. end
  29.  
  30. #Create new tasks and users
  31. Task.create!(taskname:'Install SQL Server 2017 on Windows', user:'Andrea', duedate: '2017-07-01')
  32. Task.create!(taskname:'Upgrade from SQL Server 2014 to 2017', user:'Meet', duedate: '2017-07-01')
  33. Task.create!(taskname:'Write new SQL Server content', user:'Luis', duedate: '2017-07-01')
  34. pp "Created new tasks:"
  35. pp Task.all
  36.  
  37. #Update due date for specific task
  38. task_to_update = Task.where(taskname: 'Install SQL Server 2017 on Windows').where(user: 'Andrea').first
  39. puts "Updating the following task:"
  40. pp task_to_update
  41. task_to_update.update_attribute(:duedate, '2017-07-31')
  42. puts "Due date changed:"
  43. pp task_to_update
  44.  
  45. #Destroy all tasks for specific user
  46. tasks_to_delete = Task.where(user: 'Meet').first
  47. puts "Deleting all tasks for user:"
  48. pp tasks_to_delete
  49. tasks_to_delete.destroy!
  50.  
  51. #Read all tasks
  52. puts "Printing all tasks:"
  53. pp Task.all
  54.  
  55. ActiveRecord::Base.connection.close
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement