#!/usr/bin/env ruby # datamapper woes # line 44: post.tags << t1 # => # # line 69: book.authors == [ a1 ] # => true # line 74: book.authors == [ a2 ] # => false # line 75: book.authors # => [#, #] require 'rubygems' require 'dm-core' # on both 0.9.6 and 0.9.7[ba7a38] class Tagging include DataMapper::Resource property :id, Serial belongs_to :post belongs_to :tag end class Post include DataMapper::Resource property :id, Serial has n, :taggings has n, :tags, :through => :taggings end class Tag include DataMapper::Resource property :id, Serial has n, :taggings has n, :posts, :through => :taggings end # DataMapper::Logger.new(STDERR, :debug) DataMapper.setup(:default, 'sqlite3::memory:') DataMapper.auto_migrate! post = Post.create t1 = Tag.create t2 = Tag.create begin post.tags << t1 rescue => e puts "line #{__LINE__}: post.tags << t1 # => #{e.inspect}" end class Book include DataMapper::Resource property :id, Serial has n, :authors, :through => Resource end class Author include DataMapper::Resource property :id, Serial has n, :books, :through => Resource end DataMapper.auto_migrate! book = Book.create a1 = Author.create a2 = Author.create book.authors << a1 book.save book.reload puts "line #{__LINE__}: book.authors == [ a1 ] # => #{book.authors == [ a1 ]}" book.authors = [ a2 ] book.save book.reload puts "line #{__LINE__}: book.authors == [ a2 ] # => #{book.authors == [ a2 ]}" puts "line #{__LINE__}: book.authors # => #{book.authors.inspect}"