Advertisement
Guest User

Untitled

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