Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
#!/usr/bin/env ruby require 'rubygems' gem 'dm-core', '>=0.9.1' require 'dm-core' DataMapper::Logger.new(STDOUT, :debug) DataMapper.setup(:sqlite3, 'sqlite3::memory:') class Comment include DataMapper::Resource property :id, Integer, :serial => true property :commentable_class, String property :commentable_id, Integer property :text, Text auto_migrate! :sqlite3 end class Post include DataMapper::Resource property :id, Integer, :serial => true property :title, String end repository(:sqlite3) do |repository| Post.has Post.n, :comments, :child_key => [ :commentable_id ], Comment.commentable_class => 'Post' Comment.belongs_to :post, :child_key => [ :commentable_id ], Post.comments.commentable_class => 'Post' Comment.auto_migrate!(repository.name) Post.auto_migrate!(repository.name) post = Post.create(:title => 'My first blog post!') puts '=' * 80 post.comments.create(:text => 'Click here for cheap viagra') puts '=' * 80 puts "\nFrom the parent to the child" puts '-' * 80 post = Post.first puts "Post: #{post.inspect}" puts "Comment: #{post.comments.first.inspect}" puts "\nFrom the child to the parent" puts '-' * 80 comment = Comment.first puts "Comment: #{comment.inspect}" puts "Post: #{comment.post.inspect}" end
This paste will be private.
From the Design Piracy series on my blog: