Advertisement
Guest User

Untitled

a guest
Apr 21st, 2017
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. require 'active_record'
  2. require 'tiny_tds'
  3. require 'activerecord-sqlserver-adapter'
  4.  
  5. ActiveRecord::Base.establish_connection(
  6. :adapter=> "sqlserver",
  7. :host => "localhost",
  8. :username => "sa",
  9. :password => "your_password",
  10. :database => "SampleDB"
  11. )
  12.  
  13. ActiveRecord::Schema.define do
  14. create_table :articles, force: true do |t|
  15. t.string :title
  16. t.string :author
  17. end
  18. end
  19.  
  20. class Article < ActiveRecord::Base
  21. def to_s
  22. "Title: #{title}, Author:#{author}"
  23. end
  24. end
  25.  
  26. #Creating a new article
  27. Article.create(title:'ORM', author:'Andrea')
  28. Article.create(title:'test', author:'fake')
  29.  
  30. #Fetching a first article
  31. article = Article.first
  32. puts article.to_s
  33.  
  34. #Fetching article by author
  35. fake_article = Article.find_by(author: 'fake')
  36. puts fake_article.to_s
  37.  
  38. #Destroy an article
  39. Article.find_by(author: 'fake').destroy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement