Guest User

Untitled

a guest
Mar 14th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. require "rubygems"
  2. require "dm-core"
  3. require "dm-constraints"
  4.  
  5. DataMapper.setup(:default, {
  6. :adapter => 'mysql',
  7. :database => "constraint_test",
  8. :username => 'root',
  9. :password => '',
  10. :host => '127.0.0.1',
  11. :encoding => 'UTF-8'
  12. })
  13.  
  14. class Post
  15. include DataMapper::Resource
  16. has n, :comments, :constraint => :destroy
  17.  
  18. property :id, Serial
  19. property :body, Text
  20. end
  21.  
  22. class Comment
  23. include DataMapper::Resource
  24.  
  25. belongs_to :post
  26.  
  27. property :id, Serial
  28. property :body, Text
  29. end
  30.  
  31. DataMapper.auto_migrate!
  32.  
  33. post = Post.create(:body => "hello world")
  34. comments = post.comments.create(:body => "hello brazil")
  35. comments = post.comments.create(:body => "hello canada")
  36.  
  37. puts "the post has #{post.comments.length} comments"
  38. post.destroy
  39. puts "after destroy the post now has #{post.comments.length} comments"
Add Comment
Please, Sign In to add comment