Guest User

Untitled

a guest
Jul 17th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. require_relative "connection"
  2.  
  3. ActiveRecord::Schema.define do
  4. drop_table :comments if ActiveRecord::Base.connection.data_source_exists? :comments
  5. drop_table :articles if ActiveRecord::Base.connection.data_source_exists? :articles
  6.  
  7. create_table :articles do |table|
  8. table.string :title
  9. table.string :author
  10. end
  11. create_table :comments do |table|
  12. table.references :article, foreign_key: true
  13. table.text :content
  14. table.string :email
  15. end
  16. end
  17.  
  18. class Article < ActiveRecord::Base
  19. has_many :comments
  20. before_destroy :clean_up_comments
  21.  
  22. private
  23. def clean_up_comments
  24. comments.destroy_all
  25. end
  26. end
  27.  
  28. class Comment < ActiveRecord::Base
  29. belongs_to :article
  30. end
  31.  
  32. article = Article.create author: 'Mrs. Loveshercats', title: "My cats are angels"
  33.  
  34. counterpoint = article.comments.create email: 'OhNo@You.Didnt', content: 'Your cats are abominable, hateful, spiteful, soulless poop demons.'
  35.  
  36. article.destroy!
Add Comment
Please, Sign In to add comment