Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require "rubygems"
- require "dm-core"
- require "dm-constraints"
- DataMapper.setup(:default, {
- :adapter => 'mysql',
- :database => "constraint_test",
- :username => 'root',
- :password => '',
- :host => '127.0.0.1',
- :encoding => 'UTF-8'
- })
- class Post
- include DataMapper::Resource
- has n, :comments, :constraint => :destroy
- property :id, Serial
- property :body, Text
- end
- class Comment
- include DataMapper::Resource
- belongs_to :post
- property :id, Serial
- property :body, Text
- end
- DataMapper.auto_migrate!
- post = Post.create(:body => "hello world")
- comments = post.comments.create(:body => "hello brazil")
- comments = post.comments.create(:body => "hello canada")
- puts "the post has #{post.comments.length} comments"
- post.destroy
- puts "after destroy the post now has #{post.comments.length} comments"
Add Comment
Please, Sign In to add comment