Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Created June 8, 2009 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/125713 to your computer and use it in GitHub Desktop.
Save anonymous/125713 to your computer and use it in GitHub Desktop.
require 'dm-core'
# logging if you want it
# DataObjects::Sqlite3.logger = DataObjects::Logger.new(STDOUT, 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 => Proc.new { |r, p| rand(10000).to_s }
has n, :people_things
has n, :things, :through => :people_things
end
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]
end
class Thing
include DataMapper::Resource
property :uid, Serial
has n, :people_things
has n, :people, :through => :people_things
end
# create database and tables
DataMapper.setup(:default, 'sqlite3::memory:')
DataMapper.auto_migrate!
# Fill our database
thing = Thing.create
another_thing = Thing.create
person = Person.create
pt = PeopleThing.new(:relationship => "Something")
thing.people_things << pt
person.people_things << pt
pt.save
# 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