require "rubygems" require "dm-core" DataMapper.setup(:default, 'sqlite3::memory:') class Post include DataMapper::Resource property :ID, Integer, :serial => true end class Comment include DataMapper::Resource property :comment_ID, Integer, :serial => true property :comment_post_ID, Integer property :text, String end DataMapper.auto_migrate! # You have to do has n, belongs_to AFTER auto_migrate otherwise it creates a post_id field for you. ### @export "custom-keys" class Post has n, :comments, :parent_key => [:ID], :child_key => [:comment_ID] end class Comment belongs_to :post, :parent_key => [:comment_post_ID], :child_key => [:comment_ID] end ### @end post = Post.create post.comments << Comment.create(:text => 'xx') post.save puts Post.first.comments.first.inspect