Skip to content

Instantly share code, notes, and snippets.

Created Jun 8, 2009
What would you like to do?
require 'dm-core'
# logging if you want it
# DataObjects::Sqlite3.logger =, 0)
# set up our classes
class Person
include DataMapper::Resource
# Change this property type to 'Serial' and this script works fine!
property :uid, String, :key => true, :default => { |r, p| rand(10000).to_s }
has n, :people_things
has n, :things, :through => :people_things
class PeopleThing
include DataMapper::Resource
property :id, Serial
property :person_uid, String
property :thing_uid, Integer
property :relationship, String
belongs_to :person, :child_key => [:person_uid]
belongs_to :thing, :child_key => [:thing_uid]
class Thing
include DataMapper::Resource
property :uid, Serial
has n, :people_things
has n, :people, :through => :people_things
# create database and tables
DataMapper.setup(:default, 'sqlite3::memory:')
# Fill our database
thing = Thing.create
another_thing = Thing.create
person = Person.create
pt = => "Something")
thing.people_things << pt
person.people_things << pt
# the actual code that doesn't work
things = Thing.all
puts things[0] == thing # => true
# should output true
puts things[0].people[0] == thing.people[0] # => false
# should output 1
puts things[0].people.length # => 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment