Advertisement
Guest User

Untitled

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